Java API 操作HDFS文件大集合

public class HdfsTest02 {

    static Configuration conf=new Configuration();


//创建文件并写入
   @Test
    public void create() throws IOException, URISyntaxException {
       FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
       FSDataOutputStream outputStream = fs.create(new Path("/user/new12/afdsd.txt"));
       outputStream.write("hello hadoop".getBytes());
       outputStream.flush();
       outputStream.close();
   }

    /**
     * 查看HDFS文件的内容
     * @throws Exception
     */
    @Test
    public void cat() throws Exception {
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
        FSDataInputStream inputStream = fs.open(new Path("/user/new12/a.txt"));
        IOUtils.copyBytes(inputStream,System.out,1024);
        inputStream.close();

    }

    /**
     * 删除文件
     * @throws Exception
     */
    @Test
    public void delete() throws Exception {
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
        fs.delete(new Path("/user/new12/a.txt"),true);
    }

    /**
     * 文件重命名
     * @throws Exception
     */
    @Test
    public void rename() throws Exception {
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
        Path oldPath = new Path("/user/new12/a.txt");
        Path newPath = new Path("/user/new12/b.txt");
        fs.rename(oldPath,newPath);
    }

    /**
     * 上传本地小文件到HDFS
     * @throws Exception
     */
    @Test
    public void copyFromLocalFile() throws Exception {
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
        Path localPath = new Path("F:\\sl");
        //ls为小文件的一个集合
        Path hdfsPath = new Path("/user/new12");

        fs.copyFromLocalFile(localPath,hdfsPath);
    }

    /**
     * 上传本地大文件且需要进度条
     * @throws Exception
     */
    @Test
    public void copyFromLocalFileWithProgress() throws Exception {
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
        InputStream in = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\3HDFS讲义.doc")));
        FSDataOutputStream outputStream = fs.create(new Path("/user/new12/asd.doc"),
                new Progressable() {
                    @Override
                    public void progress() {
                        System.out.print(".");   // 带进度提醒
                    }
                });
        IOUtils.copyBytes(in,outputStream,4096);
    }


    /**
     * 下载HDFS文件到本地
     * @throws Exception
     */
    @Test
    public void copyToLocalFile() throws Exception {
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
        Path localPath = new Path("F:\\sl");
        Path hdfsPath = new Path("/user/new12/asd.doc");
        fs.copyToLocalFile(hdfsPath,localPath);
    }


}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值