【发布时间】:2015-09-04 13:17:10
【问题描述】:
我已经搜索了一段时间,但似乎没有一个解决方案适合我。
非常简单 - 我想使用 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集群,但是没有报错,也没有抛出异常。我为org.apache.hadoop 包启用了TRACE 日志记录。我看到以下输出:
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:(09/15/2015)
我已经删除了 2 个 Hadoop 依赖项 - 我现在只使用一个:
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 命令,但带有额外的对象/配置。
-
我已经使用 Maven Shade 插件将我的程序打包为一个胖罐子。你指的是别的东西吗?如果是这样,请澄清。