Educoder 分布式文件系统HDFS-通关答案

第1关:HDFS的基本操作

开启hdfs
start-hdfs.sh

在HDFS中创建/usr/output/文件夹;
hdfs dfs -mkdir -p /usr/output

在本地创建hello.txt文件并添加内容:HDFS的块比磁盘的块大,其目的是为了最小化寻址开销。(标点符号为中文标点,敲击成英文标点会报错)

vim /usr/output/hello.txt 将hello.txt上传至HDFS的/usr/output/目录下;
hdfs dfs -put hello.txt /usr/output 保险起见,查看文件是否上传成功
hdfs dfs -cat /usr/output/hello.txt 删除HDFS的/user/hadoop目录;
hdfs dfs -rm -r /user/hadoop 将Hadoop上的文件hello.txt从HDFS复制到本地/usr/local目录。
hdfs dfs -copyToLocal /usr/output/hello.txt /usr/local
测评

第2关:HDFS-JAVA接口之读取文件

```java
 public class FileSystemCat { 
public static void main(String[] args) throws IOException {
	//请在Begin-End之间添加你的代码,完成任务要求。
    /

```java
********* 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 *********/
}


123456789101112131415161718 
} 

第3关:HDFS-JAVA接口之上传文件

首先打开命令行模式 mkdir -p /develop/input vim /develop/input/hello.txt
编写模式下输入 迢迢牵牛星,皎皎河汉女。 纤纤擢素手,札札弄机杼。 终日不成章,泣涕零如雨。 河汉清且浅,相去复几许? 盈盈一水间,脉脉不得语。
《迢迢牵牛星》

esc :wq 回到代码模式

 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 *********/
}
}

第4关:HDFS-JAVA接口之删除文件

public class FileSystemDelete { 
public static void main(String[] args) throws IOException {
	//请在 Begin-End 之间添加代码,完成本关任务。
    /********* Begin *********/
    String hadoop = "hdfs://localhost:9000/user/hadoop";
    String test = "hdfs://localhost:9000/tmp/test";
    String root = "hdfs://localhost:9000/";
    String tmp = "hdfs://localhost:9000/tmp";

    Configuration config = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(root), config);

    fs.delete(new Path(hadoop), true);
    fs.delete(new Path(test), true);

    Path[] paths = {new Path(root), new Path(tmp)};
    FileStatus[] status = fs.listStatus(paths);
    Path[] listPaths = FileUtil.stat2Paths(status);

    for (Path path: listPaths) {
        System.out.println(path);
    }
	/********* End *********/
}
}
  • 25
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值