HDFS的上传下载API

代码实例

package oa.epoint.com.hdfs;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Scanner;

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

public class HdfsTest {

    public static void upload() throws Exception {
        Configuration conf = new Configuration();
        URI uri = new URI("hdfs://100.2.5.1:8020");
        FileSystem fs = FileSystem.get(uri, conf, "HDFS");
        Path resP = new Path("D://files/file1.txt");
        Path destP = new Path("/tmp");

        if (!fs.exists(destP)) {
            fs.mkdirs(destP);
        }
        fs.copyFromLocalFile(resP, destP);
        fs.close();
        System.out.println("***********************");
        System.out.println("上传成功!");

    }

    public static void download() throws Exception {
        Configuration conf = new Configuration();
        String dest = "hdfs://100.2.5.1/tmp/file1.txt";
        String local = "D://files/downloads/file1.txt";
        FileSystem fs = FileSystem.get(URI.create(dest), conf, "hdfs");
        FSDataInputStream fsdi = fs.open(new Path(dest));
        OutputStream output = new FileOutputStream(local);
        IOUtils.copyBytes(fsdi, output, 4096, true);
        System.out.println("***********************");
        System.out.println("下载成功!");
    }

    public static void main(String[] args) throws Exception {
        String arg = "";
        Scanner scanner = new Scanner(System.in);
        System.out.println("1、上传文件");
        System.out.println("2、下载文件");
        boolean isconture = true;
        while (isconture) {
            arg = scanner.next();
            if ("1".equals(arg)) {
                upload();
            } else if ("2".equals(arg)) {
                download();
            }
            System.out.println("是否继续 y/n");
            if (scanner.next().equals("n")) {
                isconture = false;
            }
        }
        scanner.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值