【发布时间】:2011-06-30 13:55:16
【问题描述】:
有没有办法使用 MapReduce 生成排列?
输入文件:
1 title1
2 title2
3 title3
我的目标:
1,2 title1,title2
1,3 title1,title3
2,3 title2,title3
【问题讨论】:
标签: hadoop mapreduce permutation combinations
有没有办法使用 MapReduce 生成排列?
输入文件:
1 title1
2 title2
3 title3
我的目标:
1,2 title1,title2
1,3 title1,title3
2,3 title2,title3
【问题讨论】:
标签: hadoop mapreduce permutation combinations
由于文件将具有n 输入,因此排列应该具有n^2 输出。您可以让n 任务执行这些操作中的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);
}
}
}
【讨论】: