我正在尝试将第一个代码从HDFS复制到本地文件系统。但是,我无法复制,即使文件是预设的,我也得到例外:FileNotFoundExceptoin
。
这是我的代码: -
public class Exercise1 {
public static void main(String[] args) throws IOException ,URISyntaxException {
// TODO Auto-generated method stub
Path glob = new Path("/training/exercises/filesystem/*.txt");
String localRoot = "/home/hadoop/Training/play_area/exercises/filesystem/e1/";
FileSystem hdfs = FileSystem.get(new Configuration());
FileStatus [] files = hdfs.globStatus(glob);
for (FileStatus file : files ) {
Path from = file.getPath();
Path to = new Path(localRoot, file.getPath().getName());
System.out.println("Copying hdfs file [" + from + "] to local [" + to + "]");
hdfs.copyToLocalFile(from, to);
}
}
}
我得到的错误是: -
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.io.FileNotFoundException: File /training/exercises/filesystem does not exist
at org.apache.hadoop.fs.RawLocalFileSystem.listStatus(RawLocalFileSystem.java:315)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1281)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1321)
at org.apache.hadoop.fs.ChecksumFileSystem.listStatus(ChecksumFileSystem.java:557)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1281)
at org.apache.hadoop.fs.FileSystem.listStatus(FileSystem.java:1358)
at org.apache.hadoop.fs.FileSystem.globStatusInternal(FileSystem.java:1488)
at org.apache.hadoop.fs.FileSystem.globStatus(FileSystem.java:1443)
at org.apache.hadoop.fs.FileSystem.globStatus(FileSystem.java:1421)
at hdfs.javaAPI.Exercise1.main(Exercise1.java:24)
帮我解决此错误。
答案 0 :(得分:0)
我已经在我的机器上测试了您的代码,没有错误,没有异常,并且成功地将文件从hdfs复制到本地。对不起,我帮不了你。请仔细检查hdfs上的文件。
答案 1 :(得分:0)
当你用'/'开始你的路径时,你的意思是你的路径从根开始。您应该使用完整的URI。将您的路径从'/training/exercises/filesystem/.txt'更改为'/ home / hadoop / training / exercises / filesystem / .txt'
答案 2 :(得分:0)
我有一些类似的错误,可能这会对你有帮助,只需创建一个名为log4j.properties的文件。然后,使用此代码填充文件
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
请确保此代码在文件中是正确的
答案 3 :(得分:0)
正如StackTrace通知您的那样,它实际上是在读取本地文件系统(RawLocalFileSystem.listStatus),而不是读取hdfs。这可能是由于缺乏配置。尝试将hadoop配置文件作为资源添加到对象Configuration:
Configuration conf = new Configuration();
conf.addResource(new Path("/etc/hadoop/conf/core-site.xml"));
conf.addResource(new Path("/etc/hadoop/conf/hdfs-site.xml"));
FileSystem hdfs = FileSystem.get(conf);