本文主要是介绍序列化案例实操(统计每一个手机号耗费的总上行流量、总下行流量、总流量),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 序列化概述
- 自定义bean对象实现序列化接口(Writable)
- 案例需求
- 编写MapReduce程序
- 运行结果
序列化概述
序列化就是把内存中的对象,转换成字节序列(或其他数据传输协议)以便于存储到磁盘(持久化)和网络传输。
反序列化就是将收到字节序列(或其他数据传输协议)或者是磁盘的持久化数据,转换成内存中的对象。
自定义bean对象实现序列化接口(Writable)
在企业开发中往往常用的基本序列化类型不能满足所有需求,比如在Hadoop框架内部传递一个bean对象,那么该对象就需要实现序列化接口。
具体实现bean对象序列化步骤如下7步:
(1)必须实现Writable接口
(2)反序列化时,需要反射调用空参构造函数,所以必须有空参构造
public FlowBean() {super();
}
(3)重写序列化方法
@Override
public void write(DataOutput out) throws IOException {out.writeLong(upFlow);out.writeLong(downFlow);out.writeLong(sumFlow);
}
(4)重写反序列化方法
@Override
public void readFields(DataInput in) throws IOException {upFlow = in.readLong();downFlow = in.readLong();sumFlow = in.readLong();
}
(5)注意反序列化的顺序和序列化的顺序完全一致
(6)要想把结果显示在文件中,需要重写toString(),可用"\t"分开,方便后续用。
(7)如果需要将自定义的bean放在key中传输,则还需要实现Comparable接口,因为MapReduce框中的Shuffle过程要求对key必须能排序。
@Override
public int compareTo(FlowBean o) {// 倒序排列,从大到小return this.sumFlow > o.getSumFlow() ? -1 : 1;
}
案例需求
统计每一个手机号耗费的总上行流量、总下行流量、总流量
输入总数据:
输入数据格式:
7 13560436666 120.196.100.99 1116 954 200
id 手机号码 网络ip 上行流量 下行流量 网络状态码
期望输出数据格式:
13560436666 1116 954 2070
手机号码 上行流量 下行流量 总流量
编写MapReduce程序
FlowBean:
package com.atxiaoyu.xuliehua;import org.apache.hadoop.io.Writable;import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;public class FlowBean implements Writable {private long upFlow; //上行流量private long downFlow; //下行流量private long sumFlow; //总流量//空参构造public FlowBean() {}public long getUpFlow() {return upFlow;}public void setUpFlow(long upFlow) {this.upFlow = upFlow;}public long getDownFlow() {return downFlow;}public void setDownFlow(long downFlow) {this.downFlow = downFlow;}public long getSumFlow() {return sumFlow;}public void setSumFlow(long sumFlow) {this.sumFlow = sumFlow;}public void setSumFlow() {this.sumFlow = this.upFlow+this.downFlow;}@Overridepublic void write(DataOutput out) throws IOException {out.writeLong(upFlow);out.writeLong(downFlow);out.writeLong(sumFlow);}@Overridepublic void readFields(DataInput in) throws IOException {this.upFlow=in.readLong();this.downFlow=in.readLong();this.sumFlow=in.readLong();}@Overridepublic String toString() {return upFlow+"\t"+downFlow+"\t"+sumFlow;}
}
FlowMapper:
package com.atxiaoyu.xuliehua;import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class FlowMapper extends Mapper<LongWritable, Text,Text,FlowBean> {private Text outK=new Text();private FlowBean outV=new FlowBean();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {// 获取一行String line=value.toString();//切割String[] split=line.split("\t");//抓取想要的数据String phone=split[1];String up=split[split.length-3]; //上行流量String down=split[split.length-2]; //下行流量//封装outK.set(phone);outV.setUpFlow(Long.parseLong(up));outV.setDownFlow(Long.parseLong(down));outV.setSumFlow();// 写出context.write(outK,outV);}
}
FlowReducer:
package com.atxiaoyu.xuliehua;import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class FlowReducer extends Reducer<Text,FlowBean,Text,FlowBean> {private FlowBean outV=new FlowBean();@Overrideprotected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {//遍历集合累加值long totalUp=0;long totalDown=0;for (FlowBean value : values) {totalUp=totalUp+value.getUpFlow();totalDown=totalUp+value.getDownFlow();//封装outK,outVoutV.setUpFlow(totalUp);outV.setDownFlow(totalDown);outV.setSumFlow();//写出context.write(key,outV);}}
}
FlowDriver:
package com.atxiaoyu.xuliehua;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.kerby.config.Conf;import java.io.IOException;public class FlowDriver {public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {Configuration conf = new Configuration();//1 获取jobJob job = Job.getInstance(conf);//2 设置jar包路径job.setJarByClass(FlowDriver.class);// 3 管理mapper和reducerjob.setMapperClass(FlowMapper.class);job.setReducerClass(FlowReducer.class);// 4 设置map输出的kv类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(FlowBean.class);//5 设置最终输出的kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(FlowBean.class);//6 设置输入路径和输出路径FileInputFormat.setInputPaths(job, new Path("D:\\input"));FileOutputFormat.setOutputPath(job, new Path("D:\\output"));//7 提交jobboolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}
运行结果
与我们设想的输出结果一致。
这篇关于序列化案例实操(统计每一个手机号耗费的总上行流量、总下行流量、总流量)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!