Skip to content

Using Hadoop HDFS with Java Application

Reading files from HDFS

  • Namenode: hadoop-master:9000

  • datanode: node-1, node-2, node-3, node-4

public class HdfsFileReader {

    private static final String NAME_NODE = "hdfs://hadoop-master:9000";//nameNomeHost = localhost if you use hadoop in local mode

    public static void main(String[] args) throws URISyntaxException, IOException {
        String fileInHdfs = "/user/hadoop/books/alice.txt";
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fs = FileSystem.get(new URI(NAME_NODE), conf);
        String fileContent = IOUtils.toString(fs.open(new Path(fileInHdfs)), "UTF-8");
        System.out.println("File content - " + fileContent);
    }

}