HDFS使用流的方式上传下载

主代码

package api;


import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

/**
 * 使用流的方式上传下载
 * @author potter
 *
 */
public class HDFS_GET_AND_PUT {

    public static void main(String[] args) throws Exception {
        
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://potter2:9000");
        System.setProperty("HADOOP_USER_NAME","potter");
        FileSystem fs = FileSystem.get(conf);
        
        //上传
//        fs.copyFromLocalFile(new Path("D:/aaa.txt"), new Path("/"));
        //下载
//        fs.copyToLocalFile(new Path("/aaa.txt"), new Path("D:/sss.txt"));
        
        /**
         * 使用流的方式上传文件
         */
        //读取本地文件
        InputStream in = new FileInputStream("D:/111.zip");
        //在Hdfs上创建一个文件,返回输出流
        OutputStream out = fs.create(new Path("/111.zip"));
        //输入 ---》  输出
        IOUtils.copyBytes(in, out, 4096, true);
        
        /**
         * 使用流的方式下载文件
         */
        //读取HDFS上的文件
        /*InputStream in = fs.open(new Path("/words.txt"));
        //在本地创建一个文件,返回输出流
        OutputStream out = new FileOutputStream("D:/word.txt");
        IOUtils.copyBytes(in, out, 4096, true);*/
        
        fs.close();
    }
}
原文地址:https://www.cnblogs.com/sangumaolu/p/8545619.html