hadoop—使用java以编程方式读取存储在hdfs中的文本文件的内容

5hcedyr0  于 2021-06-03  发布在  Hadoop
关注(0)|答案(4)|浏览(299)

如何运行这个简单的java程序从hdfs中的directory/words中存储的文本文件中读取字节?我需要为此创建一个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]);
        }
    }
}
axkjgtzd

axkjgtzd1#

您可以使用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);
2fjabf4q

2fjabf4q2#

首先,您需要告诉jvm url对象中的hdfs方案。通过以下方式完成:

URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());

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

hadoop filesystemhdfs

hadoop附带了一个方便的ioutils。这会让你轻松很多。

8ulbf1ek

8ulbf1ek3#

现在回信有点晚,但对将来的读者有帮助。它将迭代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");
            }
hk8txs48

hk8txs484#

您不能从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 并用的run方法编写代码 PrivilegedExceptionAction . 如果您在本地系统中,您可能不需要它。嗯!

相关问题