Java實(shí)現(xiàn)HDFS文件上傳下載
本文實(shí)例為大家分享了利用Java實(shí)現(xiàn)HDFS文件上傳下載的具體代碼,供大家參考,具體內(nèi)容如下
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> <!--依賴(lài)庫(kù)--> <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)建與刪除
//導(dǎo)包 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對(duì)象 ?? ?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 { ? ? ? ? //結(jié)束的時(shí)候,句柄還沒(méi)有釋放就進(jìn)行釋放 ?? ??? ?if (fs!=null) { ?? ??? ??? ?try { ?? ??? ??? ??? ?fs.close() ; ?? ??? ??? ?}catch (IOException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
3、文件上傳
//導(dǎo)包 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"; ? ? //本地文件不存在就報(bào)錯(cuò),并強(qiáng)制讓程序終止 ? ? if (!new File(formPath).exists()) { ?? ??? ?System.out.println(formPath +"doesn't exits"); ? ? ? ? return; ?? ?} ? ? //初始化hadoop文件系統(tǒng)的configration對(duì)象 ?? ?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)建失敗就跳過(guò) ? ? ? ?? ?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); ? ? ? ? //利用方法將本地文件復(fù)制到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{ ? ? ? ? //如果結(jié)束Hadoop文件系統(tǒng)句柄沒(méi)有關(guān)閉,利用方法進(jìn)行句柄釋放 ?? ??? ?if (null!=fs) { ?? ??? ??? ?try { ?? ??? ??? ??? ?fs.close(); ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ? ? ? ? } ? ? } }
4、文件下載
//導(dǎo)包 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對(duì)象 ? ? ? ? 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 { ? ? ? ? ? ? //如果結(jié)束Hadoop文件系統(tǒng)句柄沒(méi)有關(guān)閉,利用方法進(jìn)行句柄釋放 ? ? ? ? ? ? if (null != fs) ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? fs.close(); ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java向數(shù)據(jù)庫(kù)插入數(shù)據(jù)顯示亂碼的幾種問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于java向數(shù)據(jù)庫(kù)插入數(shù)據(jù)顯示亂碼問(wèn)題的解決方案,文章分別羅列了前臺(tái)亂碼的問(wèn)題、前臺(tái)先后臺(tái)插入數(shù)據(jù)后臺(tái)接收到的數(shù)據(jù)是亂碼以及后臺(tái)向數(shù)據(jù)庫(kù)插入數(shù)據(jù)是亂碼等幾種情況,需要的朋友可以參考下2021-11-11Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類(lèi)的問(wèn)題
這篇文章主要介紹了Spring的@Autowired加到接口上但獲取的是實(shí)現(xiàn)類(lèi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java多線(xiàn)程ThreadAPI詳細(xì)介紹
這篇文章主要介紹了Java多線(xiàn)程ThreadAPI詳細(xì)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Java Spring處理循環(huán)依賴(lài)詳解
這篇文章主要介紹了Java中的Spring如何處理循環(huán)依賴(lài),依賴(lài)指的是Bean與Bean之間的依賴(lài)關(guān)系,關(guān)于更多Spring?處理循環(huán)依賴(lài)的詳情,需要的朋友可以參考下面文章具體內(nèi)容2023-04-04一文詳解Springboot中filter的原理與注冊(cè)
這篇文章主要為大家詳細(xì)介紹了Springboot中filter的原理與注冊(cè)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們掌握SpringBoot有一定的幫助,需要的可以參考一下2023-02-02Java面向?qū)ο蠡A(chǔ)知識(shí)之抽象類(lèi)和接口
這篇文章主要介紹了Java面向?qū)ο蟮某橄箢?lèi)和接口,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-11-11macOS下Spring Boot開(kāi)發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了macOS下Spring Boot開(kāi)發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01