我已經搜索了一段時間,並沒有任何解決方案似乎適用於我。使用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