实验2-HDFS实战

第1关 HDFS基础命令实战

第2关 HDFS高级命令实战

以上两个关卡按照提示逐步完成即可

第3关 使用JAVA API读取HDFS的数据

由于关卡设置,代码仅允许手动输入,如下:

public class FileSystemCat {

        public static void main(String[] args) throws IOException {
                //请在Begin-End之间添加你的代码,完成任务要求。
        //请按照左侧的编程要求进行编写代码
                //文件地址为 "hdfs://localhost:9000/user/hadoop/task.txt"
        /********* Begin *********/
        URI uri = URI.create("hdfs://localhost:9000/user/hadoop/task.txt");
        Configuration config = new Configuration();
        FileSystem fs = FileSystem.get(uri,config);
        InputStream in = null;
        try {
                in = fs.open(new Path(uri));
                IOUtils.copyBytes(in, System.out, 2048, false);
        } catch (Exception e) {
                IOUtils.closeStream(in);
        }



                /********* End *********/
        }
}

第4关 使用JAVA API将数据写入HDFS

由于关卡设置,代码仅允许手动输入,如下:

public class FileSystemUpload {

        public static void main(String[] args) throws IOException {
                //请在 Begin-End 之间添加代码,完成任务要求。
        /********* Begin *********/
        File localPath = new File("/develop/input/hello.txt");
        String hdfsPath = "hdfs://localhost:9000/user/tmp/hello.txt";

        InputStream in = new BufferedInputStream(new FileInputStream(localPath));

        Configuration config = new Configuration();

        FileSystem fs = FileSystem.get(URI.create(hdfsPath), config);

        long fileSize = localPath.length() > 65536 ? localPath.length() / 65536 : 1;

        FSDataOutputStream out = fs.create(new Path(hdfsPath), new Progressable() {
                long fileCount = 0;

                public void progress() {
                        System.out.println("总进度" + (fileCount / fileSize) * 100 + "%");
                        fileCount++;
                }
        });
        IOUtils.copyBytes(in, out, 2048, true);



                /********* End *********/
        }
}

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值