List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Read Data from a File in HDFS

Reading Data from HDFS

Steps for Reading Data from a File in HDFS

  • Description:
    The process involves accessing and retrieving file contents stored in the Hadoop Distributed File System (HDFS) using Java APIs or HDFS shell commands. The example demonstrates programmatic access using Java code.
Source Code
  • import java.io.*;
    import java.util.*;
    import java.net.*;

    import org.apache.hadoop.fs.*;
    import org.apache.hadoop.conf.*;
    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();
    }

    // Uncomment if using JTextArea in GUI applications
    // jTextArea1.setText(dh);
    } catch (Exception e) {
    System.out.println("File not found");
    }
    }
    }
Screenshots
  • HDFS Read Data Screenshot 1
  • HDFS Read Data Screenshot 2