作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下目录结构,
Dir1
|___Dir2
|___Dir3
|___Dir4
|___File1.gz
|___File2.gz
|___File3.gz
子目录只是嵌套的,不包含任何文件
我正在尝试使用以下内容在 HDFS 上的目录中进行递归。如果它是一个目录,我将 /*
附加到路径和 addInputPath
arg[0] = "path/to/Dir1"; // given at command line
FileStatus fs = new FileStatus();
Path q = new Path(args[0]);
FileInputFormat.addInputPath(job,q);
Path p = new Path(q.toString()+"/*");
fs.setPath(p);
while(fs.isDirectory())
{
fs.setPath(new Path(p.toString()+"/*"));
FileInputFormat.addInputPath(job,fs.getPath());
}
但是代码似乎没有进入 while
循环,我得到了 not a File
异常
最佳答案
您指的if 语句在哪里?
无论如何,您可以看看这些实用方法,它们将目录中的所有文件添加到作业的输入中:
Utils:
public static Path[] getRecursivePaths(FileSystem fs, String basePath)
throws IOException, URISyntaxException {
List<Path> result = new ArrayList<Path>();
basePath = fs.getUri() + basePath;
FileStatus[] listStatus = fs.globStatus(new Path(basePath+"/*"));
for (FileStatus fstat : listStatus) {
readSubDirectory(fstat, basePath, fs, result);
}
return (Path[]) result.toArray(new Path[result.size()]);
}
private static void readSubDirectory(FileStatus fileStatus, String basePath,
FileSystem fs, List<Path> paths) throws IOException, URISyntaxException {
if (!fileStatus.isDir()) {
paths.add(fileStatus.getPath());
}
else {
String subPath = fileStatus.getPath().toString();
FileStatus[] listStatus = fs.globStatus(new Path(subPath + "/*"));
if (listStatus.length == 0) {
paths.add(fileStatus.getPath());
}
for (FileStatus fst : listStatus) {
readSubDirectory(fst, subPath, fs, paths);
}
}
}
在您的作业运行器类中使用它:
...
Path[] inputPaths = Utils.getRecursivePaths(fs, inputPath);
FileInputFormat.setInputPaths(job, inputPaths);
...
关于recursion - FileStatus 用于递归目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17618535/
我有以下目录结构, Dir1 |___Dir2 |___Dir3 |___Dir4 |___File1.gz |___File2.gz |___File3.g
这两个类之间的主要区别是什么。主要是,在什么情况下我会使用一种而不是另一种? org.apache.hadoop.hdfs.protocol 包 http://www.sching.com/javad
我想用Google新闻之类的Java开发应用程序。 为此,我从头开始,并与Nutch进行基本设置。 我已经完成安装,但是在一个命令中出现错误。 这里是关于技术的简要介绍。我在用 -nutch 1.11
我在 Kubernetes 上创建了一个包含 3 个工作线程的 Spark 集群,并附加了一个 JupyterHub 部署,以便我可以运行大量查询。 我的 parquet 文件存储在 IBM Clou
我是一名优秀的程序员,十分优秀!