HDFS使用流的方式上传下载

本文介绍了一种使用Java API通过流方式实现HDFS文件上传和下载的方法。具体包括配置Hadoop环境、创建文件系统的实例、使用输入输出流进行文件传输等关键步骤。

主代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值