- 编程实现一个类“MyFSDataInputStream”,该类继承“org.apache.hadoop.fs.FSDataInputStream”,要求如下:实现按行读取HDFS中指定文件的方法“readLine()”,如果读到文件末尾,则返回空,否则返回文件一行的文本。
-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
public class MyFSDataInputStream extends FSDataInputStream{/**
* @param args
*/
public MyFSDataInputStream(InputStream in){
super(in);
}
public static String readline(Configuration conf,String remoteFilePath){
try (FileSystem fs = FileSystem.get(conf)) {
Path remotePath = new Path(remoteFilePath);
FSDataInputStream in = fs.open(remotePath);
BufferedReader d = new BufferedReader(new InputStreamReader(in));
String line = null;
if ((line = d.readLine()) != null) {
d.close();
in.close();
return line;
}
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}}
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
conf.set("fs.default.name","hdfs://localhost:9000");
String remoteFilePath="/Test/text.txt";
System.out.println("读取文件:"+remoteFilePath);
System.out.println(MyFSDataInputStream.readline(conf, remoteFilePath));
System.out.println("\n读取完成");
}}