2011-06-30 36 views
3

有沒有一種方法可以用MapReduce生成排列組合?用MapReduce排列

輸入文件:

1 title1 
2 title2 
3 title3 

我的目標:

1,2 title1,title2 
1,3 title1,title3 
2,3 title2,title3 

回答

5

由於文件將有n輸入,排列應該有n^2輸出。這是合理的,你可以讓這些操作執行n任務。我相信你可以做到這一點(假設只有一個文件):

把你的輸入文件放入DistributedCache,以只讀方式訪問你的Mapper/Reducers。在文件的每一行上進行輸入拆分(如在WordCount中)。該映射器將因此接收一行(例如,在您的示例中爲title1)。然後從DistributedCache中的文件中讀取行併發出您的鍵/值對:將鍵用作輸入,將值作爲DistributedCache文件中的每行。

在這個模型中,你應該只需要一個Map步驟。

喜歡的東西:

public static class PermuteMapper 
     extends Mapper<Object, Text, Text, Text>{ 

    private static final IN_FILENAME="file.txt"; 

    public void map(Object key, Text value, Context context 
        ) throws IOException, InterruptedException { 

     String inputLine = value.toString(); 

     // set the property mapred.cache.files in your 
     // configuration for the file to be available 
     Path[] cachedPaths = DistributedCache.getLocalCacheArchives(conf); 
     if (cachedPaths[0].getName().equals(IN_FILENAME)) { 
     // function defined elsewhere 
     String[] cachedLines = getLinesFromPath(cachedPaths[0]); 
     for (String line : cachedLines) 
      context.emit(inputLine, line); 
     } 
    } 
    } 
+0

正是我所需要的!日Thnx – AlexeyTokar