import java.io.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
public class cat {
/**
* 读取文件内容
*/
public static void cat(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(remotePath);
BufferedReader d = new BufferedReader(new InputStreamReader(in));) {
String line;
while ((line = d.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 主函数
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String remoteFilePath = "/user/hadoop/test.txt"; // HDFS路径
try {
System.out.println("读取文件: " + remoteFilePath);
cat.cat(conf, remoteFilePath);
System.out.println("\n读取完成");a
} catch (Exception e) {
e.printStackTrace();
}
}
}