日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hadoop 2.x的DistributedCache无法工作的问题

發布時間:2025/7/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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/

總結

以上是生活随笔為你收集整理的Hadoop 2.x的DistributedCache无法工作的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。