java read a file from hdfs code example
Example 1: hdfs read file java
Configuration conf = new Configuration();
conf.addResource(new Path("C:\\hadoop-3.1.0\\etc\\hadoop\\core-site.xml"));
conf.addResource(new Path("C:\\hadoop-3.1.0\\etc\\hadoop\\hdfs-site.xml"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String filePath = "hdfs://localhost:9000/directory/file.txt";
Path path = new Path(filePath);
FileSystem fs = path.getFileSystem(conf);
FSDataInputStream inputStream = fs.open(path);
System.out.println(inputStream.available());
String line = null;
while((line = inputStream.readLine()) != null) {
System.out.println(line);
}
fs.close();
Example 2: hdfs read file java
Configuration conf = new Configuration();
Path path = new Path("hdfs file path");
FileSystem fs = FileSystem.get(path.toUri(), conf);
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(path)));
try {
String line;
line=br.readLine();
while (line != null){
System.out.println(line);
line = br.readLine();
}
} finally {
br.close();
}