zoukankan      html  css  js  c++  java
  • hadoop的hdfs文件操作实现上传文件到hdfs

    这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上、从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;
    }
    }


    详细出处参考:http://www.jb51.net/article/48104.htm

  • 相关阅读:
    [转] 一文读懂 HTTP/2 特性
    设置VS2019 支持C++17标准
    switch case 字符串表达式支持
    在Fabric实现类似Uniswap的去中心化交易所
    数据上链的原则与方式
    2.4g无线私有协议透传方案特色梳理
    无线数字麦克风解决方案小结
    高保真的音频编解码器模块及方案解析
    基于wifi的音频采集及处理解决方案小结
    基于智能降噪的助听器解决方案解析
  • 原文地址:https://www.cnblogs.com/zhwl/p/3655686.html
Copyright © 2011-2022 走看看