提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
华中师范大学大数据课程编程作业
工具:idea
前言
提示:这里可以添加本文要记录的大概内容:
提示:以下是本篇文章正文内容,下面案例可供参考
一、编写一个Java程序,打开一个HDFS中的文件,并读取其中的数据,输出到标准输出
代码:
package com.hdfs.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
public class ReadHDFSFile {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.set(“fs.defaultFS”,“hdfs://hadoop102:8020”);
FileSystem fs = FileSystem.get(conf);
Path path = new Path(“hdfs://hadoop102:8020/sansan/word.txt”);
FSDataInputStream in = new FSDataInputStream(fs.open(path));
String line = in.readLine();
while(line != null){
System.out.println(line);
line = in.readLine();
}
}
}