How to read data from file in HDFS

Description

Data from the HDFS is read using the FileSystem and InputStreamReader Object available in hadoop.fs and java.io package that reads the data as string.

import java.io.*;
import java.util.*;
import java.net.*;

import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;s
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

 

public class readData {

public static void main (String [] args) throws Exception{
try {

Configuration conf = new Configuration();

Path pt = new Path(“hdfs://localhost:54310/home/sampleinput”);

FileSystem fs = pt.getFileSystem(conf);

BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));

String line;

line = br.readLine();

String dh=””;

while (line != null) {

dh=dh+line+”\n”;

System.out.println(line);

line = br.readLine();

}

jTextArea1.setText(dh);

} catch (Exception e) {

System.out.println(“File not found”);

}

}

Input

Input For read data from file in HDFS

Output

Output For read data from file in HDFS