了解Hadoop wordcount示例

时间:2013-03-06 05:44:07

标签: apache hadoop mapreduce

示例代码在这里 http://wiki.apache.org/hadoop/WordCount

我理解逻辑,但是,我注意到在main函数中,它只指定输入和输出路径,但是,它从不指定键和值是什么。

map和reduce函数如何计算出来?

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

public void reduce(Text key, Iterable<IntWritable> values, Context context) 

2 个答案:

答案 0 :(得分:2)

由于您了解Mapper和Reducer的格式为Key1,Value1,Key2,Value2,Key1和Value1是输入键值类型,Key2和Value2是输出类型,我将解释其余的。

在主要功能中,您会看到一行说明

job.setInputFormatClass(TextInputFormat.class);

现在,这就决定了如何读取输入文件。如果您要查看TextInputFormat LineRecordReader,您会看到(在第41行)它使用{{1}}(source)将文件分解为密钥 - 值对。这里行偏移设置为键,行本身设置为值。

但是就像你说的那样,这不是自动完成的。您可以通过编写自己的自定义输入格式记录阅读器类来控制此行为。

希望这可以解决你的疑虑。

答案 1 :(得分:0)

MapperReducer类的接口强制执行mapreduce函数的类型:

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    ...
} 

public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    ...
}

它们都是K1, V1, K2, V2形式,其中K1, V1是输入键值类型,K2, V2是输出类型。