Reducer:
- package org.fansy.date922;
- import java.io.IOException;
- import org.apache.Hadoop.io.DoubleWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- public class AverageR3 extends Reducer<Text,TextPair,Text,DoubleWritable>{
- private DoubleWritable newvalue=new DoubleWritable();
- public void reduce(Text key,Iterable<TextPair> values,Context context) throws IOException,InterruptedException{
- // TODO Auto-generated method stub
- double sum= 0.0;
- int num=0;
- for(TextPair val:values){
- sum+=val.getFirst().get();
- num+=val.getSecond().get();
- }
- double aver=sum/num;
- newvalue.set(aver);
- context.write(key, newvalue);
- }
- }
TextPair:
- package org.fansy.date922;
- import java.io.DataInput;
- import java.io.DataOutput;
- import java.io.IOException;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.DoubleWritable;
- import org.apache.hadoop.io.WritableComparable;
- public class TextPair implements WritableComparable<TextPair> {
- private DoubleWritable first;
- private IntWritable second;
- public TextPair(){
- set(new DoubleWritable(),new IntWritable());
- }
- public void set(DoubleWritable longWritable, IntWritable intWritable) {
- // TODO Auto-generated method stub
- this.first=longWritable;
- this.second=intWritable;
- }
- public DoubleWritable getFirst(){
- return first;
- }
- public IntWritable getSecond(){
- return second;
- }
- @Override
- public void readFields(DataInput arg0) throws IOException {
- // TODO Auto-generated method stub
- first.readFields(arg0);
- second.readFields(arg0);
- }
- @Override
- public void write(DataOutput arg0) throws IOException {
- // TODO Auto-generated method stub
- first.write(arg0);
- second.write(arg0);
- }
- @Override
- public int compareTo(TextPair o) {
- // TODO Auto-generated method stub
- int cmp=first.compareTo(o.first);
- if(cmp!=0){
- return cmp;
- }
- return second.compareTo(o.second);
- }
- }
查看终端中的显示也可以看出的确是有combine操作的:
- 12/09/22 15:55:45 INFO mapred.JobClient: Job complete: job_local_0001
- 12/09/22 15:55:45 INFO mapred.JobClient: Counters: 22
- 12/09/22 15:55:45 INFO mapred.JobClient: File Output Format Counters
- 12/09/22 15:55:45 INFO mapred.JobClient: Bytes Written=65
- 12/09/22 15:55:45 INFO mapred.JobClient: FileSystemCounters
- 12/09/22 15:55:45 INFO mapred.JobClient: FILE_BYTES_READ=466
- 12/09/22 15:55:45 INFO mapred.JobClient: HDFS_BYTES_READ=244
- 12/09/22 15:55:45 INFO mapred.JobClient: FILE_BYTES_WRITTEN=82758
- 12/09/22 15:55:45 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=65
- 12/09/22 15:55:45 INFO mapred.JobClient: File Input Format Counters
- 12/09/22 15:55:45 INFO mapred.JobClient: Bytes Read=122
- 12/09/22 15:55:45 INFO mapred.JobClient: Map-Reduce Framework
- 12/09/22 15:55:45 INFO mapred.JobClient: Map output materialized bytes=118
- 12/09/22 15:55:45 INFO mapred.JobClient: Map input records=14
- 12/09/22 15:55:45 INFO mapred.JobClient: Reduce shuffle bytes=0
- 12/09/22 15:55:45 INFO mapred.JobClient: Spilled Records=12
- 12/09/22 15:55:45 INFO mapred.JobClient: Map output bytes=231
- 12/09/22 15:55:45 INFO mapred.JobClient: Total committed heap usage (bytes)=301727744
- 12/09/22 15:55:45 INFO mapred.JobClient: CPU time spent (ms)=0
- 12/09/22 15:55:45 INFO mapred.JobClient: SPLIT_RAW_BYTES=108
- 12/09/22 15:55:45 INFO mapred.JobClient: <SPAN style="COLOR: #ff0000">Combine input records=14</SPAN>
- 12/09/22 15:55:45 INFO mapred.JobClient: Reduce input records=6
- 12/09/22 15:55:45 INFO mapred.JobClient: Reduce input groups=6
- 12/09/22 15:55:45 INFO mapred.JobClient: <SPAN style="COLOR: #ff0000"> Combine output records=6</SPAN>
- 12/09/22 15:55:45 INFO mapred.JobClient: Physical memory (bytes) snapshot=0
- 12/09/22 15:55:45 INFO mapred.JobClient: Reduce output records=6
- 12/09/22 15:55:45 INFO mapred.JobClient: Virtual memory (bytes) snapshot=0
- 12/09/22 15:55:45 INFO mapred.JobClient: Map output records=14
- ************************
那本书上面其实最后还有提到一个 in-Mapper Combining的一个编程,但是看的不是很明白,伪代码如下:
- class Mapper
- method Initialize
- S ← new AssociativeArray
- C ← new AssociativeArray
- method Map(string t, integer r)
- S{t} ← S{t} + r
- C{t} ← C{t} + 1
- method Close
- for all term t ∈ S do
- Emit(term t, pair (S{t}, C{t}))
继续学习 MR编程中。
