mapreduce
1_bean
首先要实现(implements)接口WritableComparable +<泛型>,后 定义所需要的变量,构造方法,编写get,set方法
其次 实现比较器 ,重写compareTo(类+自定义对象)方法 ,进行升序和降序
最后进行序列化
public void write(DataOutput out) throws IOException {
out.writeUTF(word);
out.writeInt(num);
public void readFields(DataInput in) throws IOException {
this.word = in.readUTF();
this.num = in.readInt();
}
最后最后进行 toString()方法 return 参数+“/t”+参数形式
2_Mapper
首先要实现 继承(extends)Mapper+<LongWritable,Text,SortBean(这是自定义bena类的名字),NullWritable/Text>(k—v,死记吧)
之后 是 重写 map方法(LongWritable key, Text value, Context context ) throws IOException, InterruptedException
之后 是固定套路
String[] split = value.toString().split("\t");
SortBean sortBean = new SortBean();
//将数据收集到SortBean对象中
sortBean.setWord(split[0]);
sortBean.setNum(Integer.parseInt(split[1]));
//将K2和V2写入上下文中
context.write(sortBean, NullWritable.get())
3_Reduce
首先 继承 Reduce +<SortBean,NullWritable,SortBean,NullWritable>
之后 重写reduce 方法
protected void reduce(SortBean key, Iterable values, Context context)
throws IOException, InterruptedException {
//收集数据
context.write(key, NullWritable.get());
}
4_Runner
mian 方法 thorws Exception 之后
new Configuration 对象
new job .getInstance(Configuration 对象的名字,“ 自定义名字”)
//指定job所在的jar包
job.setJarbyclass(名字.class)