我是 Hadoop 新手,我正在尝试使用 Reducer 类。
所以,基本上我在网上找到了一个教程,他们的reduce类看起来像这样,
public class mapReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
IntWritable total = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, InWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException{
for (IntWritable value: values){
total += value.get();
}
context.write(key, count);
}
}
所以我想用
myCustomObj
更改总数.引用上面的例子,类似,//..
myCustomObj total = new myCustomObj();
@Override
protected void reduce(Text key, Iterable<myCustomObj> values,
Reducer<Text, InWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException{
for (myCustomObj value: values){
total.add(value);
}
context.write(key, total.getPrimaryAttribute());
}
目标:我想要的是列表 的
key -> total
hadoop 之后的对象已经完成减少。我认为上面的代码只会输出key -> primaryAttribute
.建议:如果这太乏味了,我想以 XML 格式将所需的详细信息存储在磁盘上。但是,我不确定 map reducer 背后的理论,reducer 是在服务器还是客户端计算机(映射发生的地方)中执行?如果它发生在客户端计算机上,那么我将在所有客户端计算机上都有我的 XML 文件的一小部分。我只想将所有信息集中到一台服务器中。
我希望我把我的问题说清楚了。谢谢
编辑:我试图寻找在线资源。但是hadoops有很多定制。我不知道我应该看什么。
最佳答案
为了能够减少自定义对象,首先,您的映射器应该将此对象作为值返回。假设您的对象名称是:CustomObject
映射器定义应如下所示:
public class MyMapper extends Mapper<LongWritable, Text, Text, CustomObject> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, CustomObject>.Context context) throws IOException, InterruptedException {
// do you stuff here
}
}
现在 CustomObject 本身应该实现
WritableComparable
与所有三个必需方法的接口(interface)(主要用于洗牌阶段的要求):write
- 定义对象存储到磁盘的方式readFields
- 如何从磁盘中读取存储的对象compareTo
- 定义对象的排序方式(您可以将此留空,因为在随机播放阶段仅使用键进行排序)Reducer 签名应如下所示:
public class MyReducer extends Reducer<Text, CustomObject, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<CustomObject> values,
Reducer<Text, CustomObject, Text, IntWritable>.Context context) throws IOException, InterruptedException{
// reducer code
}
}
最后,在配置作业时,您应该指定正确的输入/输出组合。
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(CustomObject.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
这应该可以解决问题。
关于java - Hadoop MapReduce,如何减少自定义对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161354/