hadoop的hdfs文件操作實(shí)現(xiàn)上傳文件到hdfs
hdfs文件操作操作示例,包括上傳文件到HDFS上、從HDFS上下載文件和刪除HDFS上的文件,大家參考使用吧
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.File;
import java.io.IOException;
public class HadoopFile {
private Configuration conf =null;
public HadoopFile(){
conf =new Configuration();
conf.addResource(new Path("/hadoop/etc/hadoop/core-site.xml"));
}
public HadoopFile(Configuration conf){
this.conf =conf;
}
public boolean sendFile(String path,String localfile){
File file=new File(localfile);
if (!file.isFile()) {
System.out.println(file.getName());
return false;
}
try {
FileSystem localFS =FileSystem.getLocal(conf);
FileSystem hadoopFS =FileSystem.get(conf);
Path hadPath=new Path(path);
FSDataOutputStream fsOut=hadoopFS.create(new Path(path+"/"+file.getName()));
FSDataInputStream fsIn=localFS.open(new Path(localfile));
byte[] buf =new byte[1024];
int readbytes=0;
while ((readbytes=fsIn.read(buf))>0){
fsOut.write(buf,0,readbytes);
}
fsIn.close();
fsOut.close();
FileStatus[] hadfiles= hadoopFS.listStatus(hadPath);
for(FileStatus fs :hadfiles){
System.out.println(fs.toString());
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean delFile(String hadfile){
try {
FileSystem hadoopFS =FileSystem.get(conf);
Path hadPath=new Path(hadfile);
Path p=hadPath.getParent();
boolean rtnval= hadoopFS.delete(hadPath, true);
FileStatus[] hadfiles= hadoopFS.listStatus(p);
for(FileStatus fs :hadfiles){
System.out.println(fs.toString());
}
return rtnval;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean downloadFile(String hadfile,String localPath){
try {
FileSystem localFS =FileSystem.getLocal(conf);
FileSystem hadoopFS =FileSystem.get(conf);
Path hadPath=new Path(hadfile);
FSDataOutputStream fsOut=localFS.create(new Path(localPath+"/"+hadPath.getName()));
FSDataInputStream fsIn=hadoopFS.open(hadPath);
byte[] buf =new byte[1024];
int readbytes=0;
while ((readbytes=fsIn.read(buf))>0){
fsOut.write(buf,0,readbytes);
}
fsIn.close();
fsOut.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
相關(guān)文章
Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼
這篇文章主要介紹了Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java 無參數(shù)構(gòu)造函數(shù)的應(yīng)用
本篇文章主要介紹了Java 無參數(shù)構(gòu)造函數(shù)的應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04windows下使用 intellij idea 編譯 kafka 源碼環(huán)境
這篇文章主要介紹了使用 intellij idea 編譯 kafka 源碼的環(huán)境,本文是基于windows下做的項(xiàng)目演示,需要的朋友可以參考下2021-10-10SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)
這篇文章主要介紹了SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02Java實(shí)現(xiàn)在PPT中創(chuàng)建SmartArt圖形的示例代碼
SmartArt其實(shí)就是一個(gè)文字的可視化工具,用戶可在PowerPoint,Word,Excel中使用該特性創(chuàng)建各種圖形圖表。本文就將為您介紹如何通過Java應(yīng)用程序在PPT中創(chuàng)建SmartArt圖形,需要的可以參考一下2023-04-04SpringCloud服務(wù)的發(fā)現(xiàn)與調(diào)用詳解
在Java微服務(wù)越來越火的今天。幾乎什么公司都在搞微服務(wù)。而使用的比較多的就是Spring?Cloud技術(shù)棧。今天就來研究一下Spring?Cloud中服務(wù)發(fā)現(xiàn)與調(diào)用的基本原理2022-07-07Triple協(xié)議支持Java異常回傳設(shè)計(jì)實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Triple協(xié)議支持Java異?;貍髟O(shè)計(jì)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12