先检查Hadoop是否启动,没有启动记得启动哦(命令:start-dfs.sh)

检查后,创建一个文件夹

mkdir /develop
mkdir /develop/input

我们进入到刚才创建的目录

创建一个hello.txt文件

编辑文件输入如下内容后,保存并退出(尽量复制下面文本,不复制的话标点符号到时候会出错)

迢迢牵牛星,皎皎河汉女。
纤纤擢素手,札札弄机杼。
终日不成章,泣涕零如雨。
河汉清且浅,相去复几许?
盈盈一水间,脉脉不得语。
《迢迢牵牛星》

接下来,我们再在HDFS创建一个目录

来到我们代码块这一块,编写我们的代码文件

代码

    //请在 Begin-End 之间添加代码,完成任务要求。
        /********* Begin *********/

        // 本地路径
    File localPath = new File("/develop/input/hello.txt");
        // hdfs的路径
        String hdfsPath = "hdfs://localhost:9000/user/tmp/hello.txt";
        // 创建一个输入流
        InputStream in = new BufferedInputStream(new FileInputStream(localPath));
        // 配置
        Configuration config = new Configuration();
        // 创建fs文件对象
        FileSystem fs = FileSystem.get(URI.create(hdfsPath),config);
        long fileSize = localPath.length() > 65536 ? localPath.length() / 65536 : 1;
        // 这个是FS的匿名内部类
        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 *********/

点击验证执行,执行好后下一关

最后修改:2021 年 07 月 14 日
如果觉得我的文章对你有用,请随意赞赏