
Hadoop MapReduce
|
169
context.write(key,
new
IntWritable(someValue));
}
}
在
MapReduce
任务中,需要指定
Reducer
类及其键与值的输出类型。
job.setReducerClass(CustomReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
6.3.5
JSON
字符串作为文本的简单性
JSON
数据便于人们阅读,它的内置模式确实有用,许多工具可以接受
JSON
数据。由
于这些原因,到处都可以看到
JSON
数据(其文件的每一行是独立的
JSON
字符串)。因
为
Hadoop
的
Text
类型可以对
JSON
字符串序列化,所以在
MapReduce
应用中采用
JSON
数据作为输入,不再需要定制可写对象。这个过程可以像在
map()
方法中正确地使用
JSONObject
那么简单。也可以创建类,使用
value.toString()
方法的值来构造更加复杂的
映射模式。
public class JSONMapper extends
Mapper<LongWritable, Text, Text, Text> {
@Override
protected void
map(LongWritable key, Text value, Context context)
throws