MapReduce-流量统计求和-FlowBean和Mapper代码编写
生活随笔
收集整理的這篇文章主要介紹了
MapReduce-流量统计求和-FlowBean和Mapper代码编写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
流量統計
需求一: 統計求和
統計每個手機號的上行流量總和,下行流量總和,上行總流量之和,下行總流量之和分析:以手機號碼作為key值,上行流量,下行流量,上行總流量,下行總流量四個字段作為value值,然后以這個key,和value作為map階段的輸出,reduce階段的輸入
Step 1: 自定義map的輸出value對象FlowBean
package cn.learn.mapreduce_flowcount;import org.apache.hadoop.io.Writable;import java.io.DataInput; import java.io.DataOutput; import java.io.IOException;public class FlowBean implements Writable {private Integer upFlow;private Integer downFlow;private Integer upCountFlow;private Integer downCountFlow;public Integer getUpFlow() {return upFlow;}public void setUpFlow(Integer upFlow) {this.upFlow = upFlow;}public Integer getDownFlow() {return downFlow;}public void setDownFlow(Integer downFlow) {this.downFlow = downFlow;}public Integer getUpCountFlow() {return upCountFlow;}public void setUpCountFlow(Integer upCountFlow) {this.upCountFlow = upCountFlow;}public Integer getDownCountFlow() {return downCountFlow;}public void setDownCountFlow(Integer downCountFlow) {this.downCountFlow = downCountFlow;}@Overridepublic String toString() {returnupFlow +"\t" + downFlow +"\t" + upCountFlow +"\t" + downCountFlow;}@Overridepublic void write(DataOutput dataOutput) throws IOException {dataOutput.writeInt(upFlow);dataOutput.writeInt(downFlow);dataOutput.writeInt(upCountFlow);dataOutput.writeInt(downCountFlow);}@Overridepublic void readFields(DataInput dataInput) throws IOException {this.upFlow = dataInput.readInt();this.downFlow = dataInput.readInt();this.upCountFlow = dataInput.readInt();this.downCountFlow = dataInput.readInt();} }Step 2: 定義FlowMapper類
package cn.learn.mapreduce_flowcount;import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class FlowCountMapper extends Mapper<LongWritable,Text,Text,FlowBean> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {//1:拆分手機號String[] split = value.toString().split("\t");String phoneNum = split[1];//2:獲取四個流量字段FlowBean flowBean = new FlowBean();flowBean.setUpFlow(Integer.parseInt(split[6]));flowBean.setDownFlow(Integer.parseInt(split[7]));flowBean.setUpCountFlow(Integer.parseInt(split[8]));flowBean.setDownCountFlow(Integer.parseInt(split[9]));//3:將k2和v2寫入上下文中context.write(new Text(phoneNum), flowBean);} }?
總結
以上是生活随笔為你收集整理的MapReduce-流量统计求和-FlowBean和Mapper代码编写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MapReduce-流量统计求和-步骤分
- 下一篇: MapReduce-流量统计求和-Red