Java實現(xiàn)HDFS文件上傳下載
更新時間:2022年06月23日 09:19:05 作者:絕域時空
這篇文章主要為大家詳細介紹了Java實現(xiàn)HDFS文件上傳下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了利用Java實現(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> <!--依賴庫--> <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 { ? ? ? ? //結(jié)束的時候,句柄還沒有釋放就進行釋放 ?? ??? ?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{ ? ? ? ? //如果結(jié)束Hadoop文件系統(tǒng)句柄沒有關(guān)閉,利用方法進行句柄釋放 ?? ??? ?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 { ? ? ? ? ? ? //如果結(jié)束Hadoop文件系統(tǒng)句柄沒有關(guān)閉,利用方法進行句柄釋放 ? ? ? ? ? ? if (null != fs) ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? fs.close(); ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼的幾種問題解決
這篇文章主要給大家介紹了關(guān)于java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼問題的解決方案,文章分別羅列了前臺亂碼的問題、前臺先后臺插入數(shù)據(jù)后臺接收到的數(shù)據(jù)是亂碼以及后臺向數(shù)據(jù)庫插入數(shù)據(jù)是亂碼等幾種情況,需要的朋友可以參考下2021-11-11Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題
這篇文章主要介紹了Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10macOS下Spring Boot開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細介紹了macOS下Spring Boot開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01