MapReduce-流量统计求和-排序-Mapper和Reducer编写
生活随笔
收集整理的這篇文章主要介紹了
MapReduce-流量统计求和-排序-Mapper和Reducer编写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義FlowMapper
package cn.learn.mapreduce_sort;import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class FlowCountSortMapper extends Mapper<LongWritable,Text,FlowBean,Text> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {FlowBean flowBean = new FlowBean();String[] split = value.toString().split("\t");//獲取手機號,作為V2String phoneNum = split[0];//獲取其他流量字段,封裝flowBean,作為K2flowBean.setUpFlow(Integer.parseInt(split[1]));flowBean.setDownFlow(Integer.parseInt(split[2]));flowBean.setUpCountFlow(Integer.parseInt(split[3]));flowBean.setDownCountFlow(Integer.parseInt(split[4]));//將K2和V2寫入上下文中context.write(flowBean, new Text(phoneNum));} }定義FlowReducer
package cn.learn.mapreduce_sort;import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class FlowCountSortReducer extends Reducer<FlowBean,Text,Text,FlowBean> {@Overrideprotected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {for (Text value : values) {context.write(value, key);}} }?
總結
以上是生活随笔為你收集整理的MapReduce-流量统计求和-排序-Mapper和Reducer编写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MapReduce-流量统计求和-排序-
- 下一篇: MapReduce-流量统计求和-排序-