gpt4 book ai didi

java - 使用JAVA列出HDFS的文件夹和文件

转载 作者:可可西里 更新时间:2023-11-01 14:42:26 27 4
gpt4 key购买 nike

我正在尝试使用 JAVA 列出 HDFS 中的所有目录和文件。

Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://ip address"), configuration);
FileStatus[] fileStatus = fs.listStatus(new Path("hdfs://ip address/user/uname/"));
Path[] paths = FileUtil.stat2Paths(fileStatus);
for(FileStatus status : fileStatus){
System.out.println(status.getPath().toString());
}

我的代码能够生成 fs 对象但卡在第 3 行,这里它尝试读取文件夹和文件的文件。我正在使用 AWS。

请帮我解决这个问题。

最佳答案

这对我有用..

public static void main(String[] args) throws IOException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000/"), conf);
FileStatus[] fileStatus = fs.listStatus(new Path("hdfs://localhost:9000/"));
for(FileStatus status : fileStatus){
System.out.println(status.getPath().toString());
}
}

输出

hdfs://localhost:9000/All.txt
hdfs://localhost:9000/department.txt
hdfs://localhost:9000/emp.tsv
hdfs://localhost:9000/employee.txt
hdfs://localhost:9000/hbase

它认为您提供的 uri 不正确。尽量按照代码去做。

如果没有设置conf那么你必须添加资源文件

conf.addResource(new Path("/home/kishore/BigData/hadoop/etc/hadoop/core-site.xml"));
conf.addResource(new Path("/home/kishore/BigData/hadoop/etc/hadoop/hdfs-site.xml"));

关于java - 使用JAVA列出HDFS的文件夹和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com