使用Java以编程方式读取存储在HDFS中的文本文件的内容

时间:2014-02-25 16:29:40

标签: java hadoop hdfs

如何运行这个简单的Java程序从HDFS中目录/单词中存储的文本文件中读取字节?我是否需要为此目的创建一个jar文件?请建议。

      import java.io.*;
      import java.net.MalformedURLException;
      import java.net.URL;
      import org.apache.hadoop.*;
      import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    public class filesystemhdfs 
    {
     public static void main(String args[]) throws MalformedURLException, IOException
     {

        byte[] b=null;
    InputStream in=null;
    in=new URL("hdfs://localhost/words/file").openStream();
    in.read(b);
    System.out.println(""+b);
     for(int i=0;i<b.length;i++)
     {
        System.out.println("b[i]=%d"+b[i]);
        System.out.println(""+(char)b[i]);
     }

    }
   }

4 个答案:

答案 0 :(得分:2)

您可以使用HDFS API,这可以从本地运行。:

Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://namenode:8020");
        FileSystem fs = FileSystem.get(configuration);
Path filePath = new Path(
                "hdfs://namenode:8020/PATH");

        FSDataInputStream fsDataInputStream = fs.open(filePath);

答案 1 :(得分:1)

首先,您需要告诉JVM URL对象中的HDFS方案。这可以通过以下方式完成:

URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());

编译Java类后,需要使用 hadoop 命令:

hadoop filesystemhdfs

Hadoop带来了方便的IOUtils。它会为你减轻很多东西。

答案 2 :(得分:0)

您无法从HDFS读取文件,因为java支持常规文件系统。您需要使用HDFS java AP我。

public static void main(String a[]) {
     UserGroupInformation ugi
     = UserGroupInformation.createRemoteUser("root");

     try {


        ugi.doAs(new PrivilegedExceptionAction<Void>() {

            public Void run() throws Exception {

               Configuration conf = new Configuration();
                    //fs.default.name should match the corresponding value 
                    // in your core-site.xml in hadoop cluster
                conf.set("fs.default.name","hdfs://hostname:9000");
                conf.set("hadoop.job.ugi", "root");

                 readFile("words/file",conf) 

                return null;
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }

}

 public static void readFile(String file,Configuration conf) throws IOException {
    FileSystem fileSystem = FileSystem.get(conf);

    Path path = new Path(file);
    if (!ifExists(path)) {
        System.out.println("File " + file + " does not exists");
        return;
    }

    FSDataInputStream in = fileSystem.open(path);

    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line = null;
    while((line = br.readLine())!= null){
        System.out.println(line);
    }
    in.close();
    br.close();
    fileSystem.close();
 }
   public static boolean ifExists(Path source) throws IOException {

    FileSystem hdfs = FileSystem.get(conf);
    boolean isExists = hdfs.exists(source);
    System.out.println(isExists);
    return isExists;
 }

我在这里尝试使用远程计算机,这就是我使用UserGroupInformation并在PrivilegedExceptionAction的run方法中编写代码的原因。如果您在本地系统中,则可能不需要它。 HTH!

答案 3 :(得分:0)

回复有点晚,但是它将对将来的读者有所帮助。它将循环访问您的HDFS目录,并读取每个文件的内容。

仅使用Hadoop客户端和Java。

Configuration conf = new Configuration();
            conf.addResource(new Path(“/your/hadoop/conf/core-site.xml"));
            conf.addResource(new Path("/your/hadoop/confhdfs-site.xml"));
            FileSystem fs = FileSystem.get(conf);
            FileStatus[] status = fs.listStatus(new Path("hdfs://path/to/your/hdfs/directory”);
            for (int i = 0; i < status.length; i++) {
                FSDataInputStream inputStream = fs.open(status[i].getPath());
                String content = IOUtils.toString(inputStream, "UTF-8");
            }