Demonstrates reading of a file from Distributed HDFS. - Java Big Data

Java examples for Big Data:HDF

Description

Demonstrates reading of a file from Distributed HDFS.

Demo Code



import java.net.URI;
import java.util.Scanner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;


public class HDFSJavaAPIReadDemo {

    public static void main(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME", "root");
        final Path path = new Path("tutorials-links.txt");
        try (final DistributedFileSystem dFS = new DistributedFileSystem() {
            {//from w w  w  .j  av  a 2 s. c  om
                initialize(new URI("hdfs://192.168.1.8:50050"),
                        new Configuration());
            }
        };
                final FSDataInputStream streamReader = dFS.open(path);
                final Scanner scanner = new Scanner(streamReader);) {

            System.out.println("File Contents: ");
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }

        }
    }
}

Related Tutorials