文件系统 listStatus 抛出 NullPointerException
FileSystem listStatus throwing NullPointerException
我正在尝试列出 HDFS 中存在的目录的内容。我尝试了以下代码:
public static void main(String[] args) throws IOException {
String uri = args[1];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri),conf);
for(int i=0;i<args.length;i++){
System.out.println("Args: " + args[i]);
}
Path[] paths = new Path[args.length];
for(int i=1; i<args.length;i++){
paths[i] = new Path(args[i]);
System.out.println(paths[i]);
}
FileStatus[] status = fs.listStatus(paths);
Path[] listedPaths = FileUtil.stat2Paths(status);
for(Path p : listedPaths){
System.out.println(p);
}
}
但我遇到了一个例外:
Exception in thread "main" java.lang.NullPointerException
at org.apache.hadoop.fs.FileSystem.fixRelativePart(FileSystem.java:2198)
at org.apache.hadoop.hdfs.DistributedFileSystem.listStatus(DistributedFileSystem.java:697)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1482)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1559)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1539)
at com.demo.ListStatusDemo.main(ListStatusDemo.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:212)
请告诉我这件事。
问题出在您的代码中:
for(int i=1; i<args.length;i++){
paths[i] = new Path(args[i]);
....
由于您的 for 循环是基于 1 的,因此您离开了路径[0] == null(未分配)
我正在尝试列出 HDFS 中存在的目录的内容。我尝试了以下代码:
public static void main(String[] args) throws IOException {
String uri = args[1];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri),conf);
for(int i=0;i<args.length;i++){
System.out.println("Args: " + args[i]);
}
Path[] paths = new Path[args.length];
for(int i=1; i<args.length;i++){
paths[i] = new Path(args[i]);
System.out.println(paths[i]);
}
FileStatus[] status = fs.listStatus(paths);
Path[] listedPaths = FileUtil.stat2Paths(status);
for(Path p : listedPaths){
System.out.println(p);
}
}
但我遇到了一个例外:
Exception in thread "main" java.lang.NullPointerException
at org.apache.hadoop.fs.FileSystem.fixRelativePart(FileSystem.java:2198)
at org.apache.hadoop.hdfs.DistributedFileSystem.listStatus(DistributedFileSystem.java:697)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1482)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1559)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1539)
at com.demo.ListStatusDemo.main(ListStatusDemo.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:212)
请告诉我这件事。
问题出在您的代码中:
for(int i=1; i<args.length;i++){
paths[i] = new Path(args[i]);
....
由于您的 for 循环是基于 1 的,因此您离开了路径[0] == null(未分配)