欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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將文件上傳到ftp服務器

    Java將文件上傳到ftp服務器

    這篇文章主要為大家詳細介紹了Java將文件上傳到ftp服務器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼的幾種問題解決

    java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼的幾種問題解決

    這篇文章主要給大家介紹了關(guān)于java向數(shù)據(jù)庫插入數(shù)據(jù)顯示亂碼問題的解決方案,文章分別羅列了前臺亂碼的問題、前臺先后臺插入數(shù)據(jù)后臺接收到的數(shù)據(jù)是亂碼以及后臺向數(shù)據(jù)庫插入數(shù)據(jù)是亂碼等幾種情況,需要的朋友可以參考下
    2021-11-11
  • Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題

    Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題

    這篇文章主要介紹了Spring的@Autowired加到接口上但獲取的是實現(xiàn)類的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java多線程ThreadAPI詳細介紹

    Java多線程ThreadAPI詳細介紹

    這篇文章主要介紹了Java多線程ThreadAPI詳細介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Java Spring處理循環(huán)依賴詳解

    Java Spring處理循環(huán)依賴詳解

    這篇文章主要介紹了Java中的Spring如何處理循環(huán)依賴,依賴指的是Bean與Bean之間的依賴關(guān)系,關(guān)于更多Spring?處理循環(huán)依賴的詳情,需要的朋友可以參考下面文章具體內(nèi)容
    2023-04-04
  • 一文詳解Springboot中filter的原理與注冊

    一文詳解Springboot中filter的原理與注冊

    這篇文章主要為大家詳細介紹了Springboot中filter的原理與注冊的相關(guān)知識,文中的示例代碼講解詳細,對我們掌握SpringBoot有一定的幫助,需要的可以參考一下
    2023-02-02
  • 詳解Mybatis通用Mapper介紹與使用

    詳解Mybatis通用Mapper介紹與使用

    目前通用mapper只支持對單表的操作,對單表的增刪改查,無需在mapper.xml寫對應的sql語句,只需要我們調(diào)用相應的接口,對于快速開發(fā)極為方便,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 閑言碎語-逐步了解Spring

    閑言碎語-逐步了解Spring

    這篇文章主要介紹了閑言碎語-逐步了解Spring,涉及Spring的誕生,簡介,作用等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java面向?qū)ο蠡A(chǔ)知識之抽象類和接口

    Java面向?qū)ο蠡A(chǔ)知識之抽象類和接口

    這篇文章主要介紹了Java面向?qū)ο蟮某橄箢惡徒涌?文中有非常詳細的代碼示例,對正在學習java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-11-11
  • macOS下Spring Boot開發(fā)環(huán)境搭建教程

    macOS下Spring Boot開發(fā)環(huán)境搭建教程

    這篇文章主要為大家詳細介紹了macOS下Spring Boot開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論