日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/6/15 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

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