首页 > 解决方案 > Problems to emulate a "hdfs dfs -ls -R /" command in java

问题描述

I'm trying to programmatically get all the files under a given path using java code

 public static List<String> listFilesFromHDFSPath(Configuration hadoopConfiguration, String hdfsPath)
            throws IOException, IllegalArgumentException {

        List<String> filePaths = new ArrayList<String>();
        Path path = new Path(hdfsPath);
        FileSystem fs = path.getFileSystem(hadoopConfiguration);
        if(fs.isDirectory(path)){
           FileStatus[] fileStatuses = fs.listStatus(path);
           for(FileStatus fileStatus : fileStatuses){
             if(fileStatus.isFile())
               filePaths.add(fileStatus.getPath().toString());
          }
        }
        else {
          filePaths.add(path.toString());
        }
        fs.close();
        return filePaths;
    }

But this approach is now working for me, the process die after the line:

FileSystem fs = path.getFileSystem(hadoopConfiguration);

So I was trying to get a "hdfs dfs -ls -R" equivalent command in java like the one in Scala:

import scala.sys.process._
val lsResult = Seq("hdfs","dfs","-ls","-R","hdfs://path/demo/").!!

Is there something similar for java?

标签: javahadoophdfshadoop2

解决方案


推荐阅读