2012-12-06 72 views
8

我想使用分佈式緩存來允許映射器訪問數據。在主,我使用的命令訪問hadoop分佈式緩存中的文件

DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf); 

凡/用戶/彼得/求CacheFile/testCache1是存在於HDFS

然後一個文件,我的設置功能如下所示:

public void setup(Context context) throws IOException, InterruptedException{ 
    Configuration conf = context.getConfiguration(); 
    Path[] localFiles = DistributedCache.getLocalCacheFiles(conf); 
    //etc 
} 

但是,這個localFiles數組始終爲空。

我最初在單主機集羣上運行測試,但我讀到這會阻止分佈式緩存工作。我試圖用一個僞分佈式,但沒有工作,要麼

我使用Hadoop 1.0.3

感謝這裏 彼得

+0

可能重複(http://stackoverflow.com/questions/12708947/文件不能正確放入分佈式緩存) – kabuko

回答

35

的問題是,我是做了以下內容:

Configuration conf = new Configuration(); 
Job job = new Job(conf, "wordcount"); 
DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf); 

由於Job構造函數創建conf實例的內部副本,所以之後添加緩存文件不會影響任何事情。相反,我應該這樣做:

Configuration conf = new Configuration(); 
DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf); 
Job job = new Job(conf, "wordcount"); 

現在它的工作。感謝hadoop用戶名單上的Harsh幫助。

+0

苛刻的確是天賜之物! ..用相同的時間浪費了很多小時。 謝謝! – Shatu

11
Configuration conf = new Configuration(); 
Job job = new Job(conf, "wordcount"); 
DistributedCache.addCacheFile(new URI("/userpetercacheFiletestCache1"),job.getConfiguration()); 

您也可以這樣做。

4

一旦作業被分配到與配置對象, 即Configuration conf = new Configuration();

Job job = new Job(conf, "wordcount"); 

然後,如果如下所示處理CONF的屬性,例如

conf.set("demiliter","|"); 

DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf); 

這樣的變化不會反映在僞集羣中或者集羣如何與當地環境協同工作。

2

此版本的代碼(與上述結構略有不同)一直適用於我。

//in main(String [] args) 
Job job = new Job(conf,"Word Count"); 
... 
DistributedCache.addCacheFile(new URI(/user/peter/cacheFile/testCache1), job.getConfiguration()); 

我沒有看到在映射代碼完整的設置()函數

public void setup(Context context) throws IOException, InterruptedException { 

    Configuration conf = context.getConfiguration(); 
    FileSystem fs = FileSystem.getLocal(conf); 

    Path[] dataFile = DistributedCache.getLocalCacheFiles(conf); 

    // [0] because we added just one file. 
    BufferedReader cacheReader = new BufferedReader(new InputStreamReader(fs.open(dataFile[0]))); 
    // now one can use BufferedReader's readLine() to read data 

} 
的[文件沒有正確地放置到分佈式緩存]
+0

謝謝@Somum,它爲我工作。我使用hadoop 1.2.1進行了檢查 –