Hadoop 2.x的DistributedCache无法工作的问题
轉自:http://www.codelast.com/?p=8131
現象:和這個帖子描述的一樣,簡單說來就是,在Hadoop 2.x上,用新的DistributedCache的API,在mapper中會獲取不到這個cache文件。
下面就詳細地描述一下新舊API的用法區別以及解決辦法。
『1』舊API
將HDFS文件添加到distributed cache中:
| 1 2 | Configuration conf = job.getConfiguration(); DistributedCache.addCacheFile(new URI(inputFileOnHDFS), conf);? // add file to distributed cache |
其中,inputFileOnHDFS是一個HDFS文件的路徑,也就是你要用作distribute cache的文件的路徑,例如 /user/codelast/123.txt
在mapper的setup()方法中:
| 1 2 3 | Configuration conf = context.getConfiguration(); Path[] localCacheFiles = DistributedCache.getLocalCacheFiles(conf); readCacheFile(localCacheFiles[0]); |
其中,readCacheFile()是我們自己的讀取cache文件的方法,可能是這樣做的(僅舉個例子):
| 1 2 3 4 5 6 7 8 | private static void readCacheFile(Path cacheFilePath) throws IOException { ??BufferedReader reader = new BufferedReader(new FileReader(cacheFilePath.toUri().getPath())); ??String line; ??while ((line = reader.readLine()) != null) { ????//TODO: your code here ??} ??reader.close(); } |
文章來源:http://www.codelast.com/
『2』新API
上面的代碼中,addCacheFile()?方法和?getLocalCacheFiles()?都已經被Hadoop 2.x標記為?@Deprecated?了。
因此,有一套新的API來實現同樣的功能,這個鏈接里有示例,我在這里再詳細地寫一下。
將HDFS文件添加到distributed cache中:
| 1 | job.addCacheFile(new Path(inputFileOnHDFS).toUri()); |
在mapper的setup()方法中:
| 1 2 3 | Configuration conf = context.getConfiguration(); URI[] localCacheFiles = context.getCacheFiles(); readCacheFile(localCacheFiles[0]); |
其中,readCacheFile()是我們自己的讀取cache文件的方法,可能是這樣做的(僅舉個例子):
| 1 2 3 4 5 6 7 8 | private static void readCacheFile(URI cacheFileURI) throws IOException { ??BufferedReader reader = new BufferedReader(new FileReader(cacheFileURI.getPath())); ??String line; ??while ((line = reader.readLine()) != null) { ????//TODO: your code here ??} ??reader.close(); } |
但是就像文章開頭的那個鏈接里所描述的問題一樣,你可能會發現?context.getCacheFiles() 總是返回null,也就是你無法讀到cache文件。
這個問題有可能是這個bug造成的,你可以對比一下你的Hadoop版本。
文章來源:http://www.codelast.com/
『3』解決辦法
(1)打patch
(2)升級Hadoop版本
(3)使用舊的DistributedCache API,經測試OK
文章來源:http://www.codelast.com/
- 上一篇:?[原創] 去除流氓插件“微度標簽頁”內置的“億起發”返利鏈接跳轉
- 下一篇:?[原創] 再談 共軛方向法/Conjugate Direction Method In Optimization
總結
以上是生活随笔為你收集整理的Hadoop 2.x的DistributedCache无法工作的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZeroCopyLiteralByteS
- 下一篇: Hadoop中的压缩Codec