本栏目主讲MapReduce设计模式,每天更新…..
数值概要模式
目的:把数据取出进行聚合,最基本的设计模式
主要用于处理数值或者计数,分类等场景,例:sql里面的group by
运行环境:windows下VM虚拟机,centos系统,hadoop2.2.0,三节点 ,java 1.7
需要处理的数据为
ID number
1 1
2 2
34 6
54 34
2 56
65 12
1 78
65 65
45 45
54 99
34 56
2 76
1 34
54 26
45 34
65 73
求出每个ID的最大number和最小number及每个ID出现的次数
注:以上数据每行的ID与number以“ ”(空格)作为分隔符
1.首先自定义一个Writable对象
(需要实现Writable接口的readField和write方法)
package boke;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable;
public class MaxMinCountWritable implements Writable{
//三个变量,分别记录最大值和最小值及出现次数
private long Max=0;
private long Min=0;
private long Count=0;
//各个变量的set和get方法
public void setMax(long Max)
{
this.Max=Max;
}
public void setMin(long Min)
{
this.Min=Min;
}
public void setCount(long Count)
{
this.Count=Count;
}
public long getMin()
{
return this.Min;
}
public long getCount()
{
return this.Count;
}
public long getMax()
{
return this.Max;
}
//这里是实现Writable接口的方法
public void readFields(DataInput in)throws IOException
{
Max=in.readLong();
Min=in.readLong();
Count=in.readLong();
}
public void write(DataOutput out)throws