设为首页 加入收藏

TOP

JAVA API 上传下载文件到HDFS
2018-12-06 00:12:01 】 浏览:20
Tags:JAVA API 上传下载 文件 HDFS
package com.kfk.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;

public class HdfsApp {

    private FileSystem getFileSystem()throws Exception{
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
        return fileSystem;
    }

    private void readHdfsFile(String filePath){
        FSDataInputStream fsDataInputStream=null;
        try {
            Path path = new Path(filePath);
            fsDataInputStream = this.getFileSystem().open(path);
            IOUtils.copyBytes(fsDataInputStream,System.out,4096,false);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fsDataInputStream != null){
                IOUtils.closeStream(fsDataInputStream);
            }
        }
    }

    private void writeHdfsFile(String inPath,String outPath){
        FSDataOutputStream fsDataOutputStream = null;
        FileInputStream fileInputStream = null;
        try {
            Path path = new Path(outPath);
            fsDataOutputStream = this.getFileSystem().create(path);
            fileInputStream = new FileInputStream(new File(inPath));
            IOUtils.copyBytes(fileInputStream,fsDataOutputStream,4096,false);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fileInputStream !=null){
                IOUtils.closeStream(fileInputStream);
            }if (fsDataOutputStream !=null){
                IOUtils.closeStream(fsDataOutputStream);
            }
        }
    }


    public static void main(String[] args){
        HdfsApp hdfsApp = new HdfsApp();
//        hdfsApp.readHdfsFile(filePath);
        String inPath="D://workSpace/src/main/resources/hdfs-site.xml";
        String outPath="hdfs://ns/user/kfk/data/local.xml";
        hdfsApp.writeHdfsFile(inPath,outPath);
        }

}


编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇centos-7 部署hadoop2.5.1 >&g.. 下一篇sqoop   把 hdfs 和关系型数..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }