日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hadoop的hdfs文件操作实现上传文件到hdfs

發布時間:2025/6/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hadoop的hdfs文件操作实现上传文件到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;
??? }
}

總結

以上是生活随笔為你收集整理的hadoop的hdfs文件操作实现上传文件到hdfs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。