我已经搜索了一段时间,并没有任何解决方案似乎适用于我。使用Java API将数据上传到HDFS
非常简单 - 我想使用Java API将数据从本地文件系统上传到HDFS。 Java程序将在已配置为通过shell与远程Hadoop群集通信的主机(即hdfs dfs -ls
等)上运行。
我已经包括在我的项目下面的依赖关系:
hadoop-core:1.2.1
hadoop-common:2.7.1
hadoop-hdfs:2.7.1
我有一些代码如下所示:
File localDir = ...;
File hdfsDir = ...;
Path localPath = new Path(localDir.getCanonicalPath());
Path hdfsPath = new Path(hdfsDir.getCanonicalPath());
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 fs = FileSystem.get(configuration);
fs.getFromLocalFile(localPath, hdfsPath);
本地数据没有被复制到Hadoop集群,但没有报告错误,也不会引发异常。我已启用TRACE
登录org.apache.hadoop
包。我看到以下输出:
DEBUG Groups:139 - Creating new Groups object
DEBUG Groups:139 - Creating new Groups object
DEBUG Groups:59 - Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping; cacheTimeout=300000
DEBUG Groups:59 - Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping; cacheTimeout=300000
DEBUG UserGroupInformation:147 - hadoop login
DEBUG UserGroupInformation:147 - hadoop login
DEBUG UserGroupInformation:96 - hadoop login commit
DEBUG UserGroupInformation:96 - hadoop login commit
DEBUG UserGroupInformation:126 - using local user:UnixPrincipal: willra05
DEBUG UserGroupInformation:126 - using local user:UnixPrincipal: willra05
DEBUG UserGroupInformation:558 - UGI loginUser:<username_redacted>
DEBUG UserGroupInformation:558 - UGI loginUser:<username_redacted>
DEBUG FileSystem:1441 - Creating filesystem for file:///
DEBUG FileSystem:1441 - Creating filesystem for file:///
DEBUG FileSystem:1290 - Removing filesystem for file:///
DEBUG FileSystem:1290 - Removing filesystem for file:///
DEBUG FileSystem:1290 - Removing filesystem for file:///
DEBUG FileSystem:1290 - Removing filesystem for file:///
任何人都可以帮助我解决此问题吗?
编辑1:(2015年9月15日)
我已经删除了Hadoop的依赖关系的2 - 我只使用一个现在:
hadoop-core:1.2.1
我的代码是现在以下:
File localDir = ...;
File hdfsDir = ...;
Path localPath = new Path(localDir.getCanonicalPath());
Path hdfsPath = new Path(hdfsDir.getCanonicalPath());
Configuration conf = new Configuration();
fs.getFromLocalFile(localPath, hdfsPath);
我以前执行我用下面的命令应用:
$ java -jar <app_name>.jar <app_arg1> <app_arg2> ...
现在我用这个命令执行它:
$ hadoop jar <app_name>.jar <app_arg1> <app_arg2> ...
有了这些变化,预期我的应用程序现在HDFS进行交互。据我所知,hadoop jar
命令仅适用于打包为可执行jar的Map Reduce作业,但这些更改对我来说是个窍门。
hadoop命令在类路径中包含hadoop类,并且如果需要将jar拆开并重新打包以包含某些库。在enh,haddop命令将执行java命令,但会使用额外的对象/配置。 – RojoSam
我已经使用Maven Shade插件将我的程序打包为一个胖罐子。你是指别的东西吗?如果是这样,请澄清。 – Kasa