我需要在java代码中实现一个快速而简单的hdfs get功能,它的工作方式与命令行“hadoop fs get”非常相似,因为它支持通配符。
使用 Filesystem.copyToLocalFile()
方法似乎不提供此功能,但正在运行 FsShell.run()
使用适当的参数似乎可以调用确切的命令行功能,但由于某些原因,它在执行get时抛出空指针异常。
我的代码如下:
Configuration conf = new Configuration();
conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
FileSystem.setDefaultUri(conf, "hdfs://192.168.61.129:8020");
FsShell hdfsShell = new FsShell(conf);
String src = "/user/andrei-test/test.txt";
String dst = "D:/temp/";
hdfsShell.run(new String[]{ "-get", src, dst });
我还注意到在做看跌期权时:
hdfsShell.run(new String[]{ "-put", "D:/temp/test.txt", "/user/andrei-test/test-put.txt" });
一切正常。
进入调试模式时,我注意到异常是从java.lang.processbuilder.start()方法引发的:
for (String arg : cmdarray)
if (arg == null)
throw new NullPointerException();
当cmd数组具有以下值时:
[null, chmod, 0644, D:\temp\test.txt._COPYING_]
从这里我有点困惑,有人遇到过这个问题吗?我也找不到在google上使用fsshell.run()执行get的例子,我觉得有点奇怪。
1条答案
按热度按时间wi3ka0sx1#
显然,这样做并不像做get via那样独立地工作
FileSystem.copyToLocalFile()
最终解决这个问题的方法是创建一个环境变量HADOOP_HOME
确保%HADOOP_HOME%\bin\winutils.exe
当时在场