#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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.

Sample Code

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”);

}

}


Screenshots