hdfs的java接口简单示例

本文提供了一个使用Java操作HDFS的示例代码,包括初始化HDFS客户端、递归删除文件或目录、创建目录、上传及下载文件等基本操作。
public class HDFSDemo {
    
    private FileSystem fs = null;
    @Before
    public void init() throws IOException, URISyntaxException, InterruptedException{
        fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration());
    }
    @Test
    //递归删除文件或者文件夹
    public void  testDel() throws IllegalArgumentException, IOException{
        boolean flag = fs.delete(new Path("/test"),true);
        System.out.println(flag);
    }
    @Test
    //创建目录
    public void testMkdir() throws IllegalArgumentException, IOException{
        boolean flag = fs.mkdirs(new Path("/Google3Papers"));
        System.out.println(flag);
    }
    @Test
    //上传文件
    public void testUpload() throws IllegalArgumentException, IOException{
        FSDataOutputStream out = fs.create(new Path("/Google3Papers/Google-Bigtable1.0.pdf"));
        FileInputStream in = new FileInputStream(new File("/Users/fengmingyue/Desktop/Google-Bigtable1.0.pdf"));
        IOUtils.copyBytes(in, out, 2048, true);
    }
    @Test
    //下载文件
    public void testDownload() throws IllegalArgumentException, IOException{
        InputStream in = fs.open(new Path("/test"));
        FileOutputStream out = new FileOutputStream(new File("/Users/fengmingyue/Desktop/ttt"));
        IOUtils.copyBytes(in, out, 2048, true);
    }
}

转载于:https://www.cnblogs.com/fengmingyue/p/6347578.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值