最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)HDFS文件上傳下載

 更新時間:2022年06月23日 09:19:05   作者:絕域時空  
這篇文章主要為大家詳細介紹了Java實現(xiàn)HDFS文件上傳下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了利用Java實現(xiàn)HDFS文件上傳下載的具體代碼,供大家參考,具體內容如下

1、pom.xml配置

<!--配置-->
<properties>
?? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
?? ?<maven.compiler.source>1.8</maven.compiler.source>
? ? <maven.compiler.target>1.8</maven.compiler.target>
? ? <hadoop.version>3.1.3</hadoop.version>
</properties>
<!--依賴庫-->
<dependencies>
?? ?<dependency>
?? ??? ?<groupId>org.apache.hadoop</groupId>
? ? ? ?? ?<artifactId>hadoop-common</artifactId>
? ? ? ?? ?<version>${hadoop.version}</version>
? ? </dependency>
? ? <dependency>
? ? ??? ?<groupId>org.apache.hadoop</groupId>
? ? ? ?? ?<artifactId>hadoop-mapreduce-client-core</artifactId>
? ? ? ?? ?<version>${hadoop.version}</version>
? ? </dependency>
</dependencies>

2、創(chuàng)建與刪除

//導包
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
public static void main( String[] args ){
? ? //初始化hadoop文件系統(tǒng)的configration對象
?? ?Configuration conf = new Configuration();
? ? //將hadoop的configration信息傳入
?? ?conf.set("fs.defaultFS","hdfs://192.168.50.102:9000");
? ? //初始化Hadoop文件系統(tǒng)的句柄
?? ?FileSystem fs=null;
? ? try {
? ? ? ? //配置Hadoop的文件句柄信息
?? ??? ?fs=FileSystem.get(conf);
? ? ? ? //定義Hadoop的文件路徑
? ? ? ? final String PATH="/test/kb16/hadoop/ratings.csv";
? ? ? ? //初始化Hadoop的路徑信息
? ? ? ? Path path = new Path(PATH);
? ? ? ? //如果文件路徑存在就刪除
?? ??? ?if (fs.exists(path)) {
?? ??? ??? ?System.out.println("DELETE "+fs.delete(path, true));
?? ??? ?}else{
? ? ? ? ? ? //如果文件路徑不存在就創(chuàng)建
?? ??? ??? ?System.out.println("CREATE "+fs.create(path));
?? ??? ?}
?? ?} catch (IOException e) {
?? ??? ?e.printStackTrace();
?? ?}finally {
? ? ? ? //結束的時候,句柄還沒有釋放就進行釋放
?? ??? ?if (fs!=null) {
?? ??? ??? ?try {
?? ??? ??? ??? ?fs.close() ;
?? ??? ??? ?}catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

3、文件上傳

//導包
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.io.IOException;
public static void main(String[] args) {
? ? //定義本地上傳文件路徑
?? ?final String formPath="E:\\ratings.csv";
? ? //本地文件不存在就報錯,并強制讓程序終止
? ? if (!new File(formPath).exists()) {
?? ??? ?System.out.println(formPath +"doesn't exits");
? ? ? ? return;
?? ?}
? ? //初始化hadoop文件系統(tǒng)的configration對象
?? ?Configuration conf = new Configuration();
? ? //將hadoop的configration信息傳入
? ? conf.set("fs.defaultFS","hdfs://192.168.50.102:9000");
? ? //初始化Hadoop文件系統(tǒng)的句柄
?? ?FileSystem fs=null;
? ? try {
? ? ? ? //將config信息傳入
?? ??? ?fs=FileSystem.get(conf);
? ? ? ? //定義上傳到HDFS的路徑
?? ??? ?final String toPath="/test/kb16/hive";
? ? ? ? //初始化路徑
?? ??? ?Path to =new Path(toPath);
? ? ? ? //如果文件路徑存在不執(zhí)行,如果文件路徑不存在就嘗試創(chuàng)建,如果創(chuàng)建失敗就跳過
? ? ? ?? ?if (!fs.exists(to)&& !fs.mkdirs(to)) {
?? ??? ??? ?System.out.println(toPath +"doesn't exit and can't be created");
?? ??? ??? ?return;
?? ??? ?}
? ? ? ? //初始化上傳文件路徑
?? ??? ?Path from=new Path(formPath);
? ? ? ? //利用方法將本地文件復制到HDFS中
?? ??? ?fs.copyFromLocalFile(from, to);
?? ??? ?System.out.println("succeed in copying from "+formPath+" to "+toPath);
?? ?} catch (IOException e) {
?? ??? ?e.printStackTrace();
?? ??? ?System.out.println("FAILURE");
?? ?}finally{
? ? ? ? //如果結束Hadoop文件系統(tǒng)句柄沒有關閉,利用方法進行句柄釋放
?? ??? ?if (null!=fs) {
?? ??? ??? ?try {
?? ??? ??? ??? ?fs.close();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
? ? ? ? }
? ? }
}

4、文件下載

//導包
import com.google.inject.internal.cglib.core.$LocalVariablesSorter;
import com.google.inject.internal.cglib.proxy.$Factory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.File;
import java.io.IOException;
public class Download {
? ? public static void main(String[] args) {
? ? ? ? //定義文件下載路徑
? ? ? ? final String toPath = "C:\\Users\\Jialin\\Desktop";
? ? ? ? //獲取路徑
? ? ? ? File to = new File(toPath);
? ? ? ? //如果路存在或者文件路徑不存在但是創(chuàng)建成功就不執(zhí)行if方法
? ? ? ? if (!to.exists()&&!to.mkdirs()) {
? ? ? ? ? ? System.err.println(toPath + "doesn't exist and can't be created");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //初始化hadoop文件系統(tǒng)的configration對象
? ? ? ? Configuration config = new Configuration();
? ? ? ? //將hadoop的configration信息傳入
? ? ? ? config.set("fs.defaultFS", "hdfs://192.168.50.102:9000");
? ? ? ? //初始化Hadoop文件系統(tǒng)的句柄
? ? ? ? FileSystem fs = null;
? ? ? ? try {
? ? ? ? ? ? //將config信息傳入
? ? ? ? ? ? fs = FileSystem.get(config);
? ? ? ? ? ? //定義下載文件路徑
? ? ? ? ? ? final String fromPath = "/test/kb16/hive/ratings.csv";
? ? ? ? ? ? //獲取路徑信息
? ? ? ? ? ? Path from = new Path(fromPath);
? ? ? ? ? ? //如果指定下載文件不存在就退出
? ? ? ? ? ? if (!fs.exists(from)) {
? ? ? ? ? ? ? ? System.err.println(toPath + "doesn't exist ");
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
?? ??? ??? ?//獲取文件下載路徑信息
? ? ? ? ? ? Path _to = new Path(toPath);
? ? ? ? ? ? //利用方法將Hadoop文件下載到本地
? ? ? ? ? ? fs.copyToLocalFile(from,_to);
? ? ? ? ? ? System.out.println("succeed in downloading from "+fromPath+" to"+toPath);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? System.out.println("FAILURE");
? ? ? ? } finally {
? ? ? ? ? ? //如果結束Hadoop文件系統(tǒng)句柄沒有關閉,利用方法進行句柄釋放
? ? ? ? ? ? if (null != fs)
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? fs.close();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java 獲取 jar包以外的資源操作

    Java 獲取 jar包以外的資源操作

    這篇文章主要介紹了Java 獲取 jar包以外的資源操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • maven安裝jar包到本地的操作方法

    maven安裝jar包到本地的操作方法

    這篇文章主要介紹了maven安裝jar包到本地,執(zhí)行該命令后,Maven?會將該?JAR?文件安裝到你本地?Maven?倉庫中,需要的朋友可以參考下
    2007-01-01
  • 基于SpringBoot解決CORS跨域的問題(@CrossOrigin)

    基于SpringBoot解決CORS跨域的問題(@CrossOrigin)

    這篇文章主要介紹了基于SpringBoot解決CORS跨域的問題(@CrossOrigin),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • maven 配置多個倉庫的方法

    maven 配置多個倉庫的方法

    這篇文章主要介紹了maven 配置多個倉庫的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • 使用Spring的FactoryBean創(chuàng)建和獲取Bean對象方式

    使用Spring的FactoryBean創(chuàng)建和獲取Bean對象方式

    這篇文章主要介紹了使用Spring的FactoryBean創(chuàng)建和獲取Bean對象方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java 8新特性方法引用詳細介紹

    Java 8新特性方法引用詳細介紹

    這篇文章主要介紹了Java 8新特性方法引用詳細介紹的相關資料,這里對新特性 方法引用做的資料整理,具有參考價值,需要的朋友可以參考下
    2016-12-12
  • Java實現(xiàn)分頁的幾種方法詳細解析

    Java實現(xiàn)分頁的幾種方法詳細解析

    這篇文章主要介紹了Java實現(xiàn)分頁的幾種方法詳細解析,在Java中想實現(xiàn)分頁功能有幾種常用的方法,今天我們就來詳細解析一下,文中提供了解決思路和部分實現(xiàn)代碼,需要的朋友可以參考下
    2023-11-11
  • 解決springboot集成swagger碰到的坑(報404)

    解決springboot集成swagger碰到的坑(報404)

    這篇文章主要介紹了解決springboot集成swagger碰到的坑(報404),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java設計模式之享元模式

    Java設計模式之享元模式

    這篇文章介紹了Java設計模式之享元模式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • SpringBoot中的Future接口詳解

    SpringBoot中的Future接口詳解

    這篇文章主要介紹了SpringBoot中的Future接口詳解,在異步編程中,我們通常需要處理一些耗時的操作,一種常見的做法是使用 Future 接口來代表一個異步操作的結果,需要的朋友可以參考下
    2023-07-07

最新評論

南宁市| 犍为县| 隆子县| 讷河市| 贺州市| 兴国县| 荆门市| 开江县| 舒城县| 磐石市| 普陀区| 丘北县| 邮箱| 黄龙县| 祁阳县| 明光市| 扎兰屯市| 霍邱县| 贡觉县| 项城市| 长兴县| 东方市| 新安县| 化州市| 沾化县| 星座| 汪清县| 海兴县| 富锦市| 平武县| 贡嘎县| 江安县| 额敏县| 松原市| 双鸭山市| 当雄县| 达拉特旗| 曲沃县| 昌吉市| 双城市| 宜章县|