【发布时间】:2013-01-15 08:59:36
【问题描述】:
我对 Hadoop 中的 MapReduce 有点陌生。我正在尝试处理来自许多日志文件的条目。 mapper 流程与WordCount 教程中的非常相似。
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
我不想把这个词作为reducer的键,而是把一个表中的相关数据放在RDBMS中。比如处理后的文字是这样的
apple orange duck apple giraffe horse lion, lion grape
还有一张桌子
name type
apple fruit
duck animal
giraffe animal
grape fruit
orange fruit
lion animal
所以,我不想计算单词,而是计算类型。输出会像
fruit 4
animal 5
假设在前面的代码中,会是这样的
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
String object = tokenizer.nextToken();
//========================================
String type = SomeClass.translate(object);
//========================================
word.set(type);
output.collect(word, one);
}
}
SomeClass.translate 将通过从 RDBMS 查询将对象名称转换为类型。
我的问题
- 这可行吗? (以及如何做到这一点?)
- 有什么顾虑?我了解到映射器将在多台机器上运行。那么假设在多台机器上有
apple字,如何减少apple的数据库查找次数? - 或者有没有在映射器中不进行翻译的非常好的选择?或者也许有一种常见的方法可以做到这一点? (或者这整个问题是一个非常愚蠢的问题?)
更新
我在 Amazon Elastic MapReduce 上使用 Apache Hadoop 实现它,转换表存储在 Amazon RDS/MySQL 中。如果您能提供一些示例代码或链接,我将不胜感激。
【问题讨论】: