Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

hdfs java read all files in directory

/**
 * @param filePath
 * @param fs
 * @return list of absolute file path present in given path
 * @throws FileNotFoundException
 * @throws IOException
 */
public static List<String> getAllFilePath(Path filePath, FileSystem fs) throws FileNotFoundException, IOException {
    List<String> fileList = new ArrayList<String>();
    FileStatus[] fileStatus = fs.listStatus(filePath);
    for (FileStatus fileStat : fileStatus) {
        if (fileStat.isDirectory()) {
            fileList.addAll(getAllFilePath(fileStat.getPath(), fs));
        } else {
            fileList.add(fileStat.getPath().toString());
        }
    }
    return fileList;
}

// Example
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String filePath = "hdfs://localhost:9000/a/dir/here";
Path path = new Path(filePath);
FileSystem fs = path.getFileSystem(conf);

filePath = Tools.getAllFilePath(new Path(filePath), fs).get(0);
System.out.println(Tools.getAllFilePath(new Path(filePath), fs));
 
PREVIOUS NEXT
Tagged: #hdfs #java #read #files #directory
ADD COMMENT
Topic
Name
3+1 =