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