1.什么是序列化
2.为什么要序列化
3.为什么不用Java的序列化
4.自定义bean对象实现序列化接口(Writable)
在企业开发中往往常用的基本序列化类型不能满足所有需求,比如在Hadoop框架内部传递一个bean对象,那么该对象就需要实现序列化接口。
具体实现bean对象序列化步骤如下7步:
1) 必须实现Writable接口
2) 反序列话时,需要反射调用无参构造方法,所以必须要有无参构造方法
3) 重写序列化方法write()
4) 重写反序列化方法readFields()
5) 反序列化的顺序和序列化的顺序务必一致
6) 要想把结果显示在文件中,需要重写toString()方法
7) 如果将自定义的Bean放在key中传输,则必须要实现comparable接口.因为MapReduce的Shuffle过程要求对key必须能排序
注:Hadoop的序列化只保存对象的指定属性数据,而Java保存的东西太多,显得太重,在传输和磁盘存储上,会造成资源的浪费和紧张
5. 序列化案例实操
如下文件,统计每一个手机号耗费的总上行流量、下行流量、总流量
1 13736230513 192.196.100.1 www.baidu.com 2481 24681 200
2 13846544121 192.196.100.2 264 0 200
3 13956435636 192.196.100.3 132 1512 200
4 13966251146 192.168.100.1 240 0 404
5 18271575951 192.168.100.2 www.baidu.com 1527 2106 200
6 84188413 192.168.100.3 www.baidu.com 4116 1432 200
7 13590439668 192.168.100.4 1116 954 200
8 15910133277 192.168.100.5 www.hao123.com 3156 2936 200
9 13729199489 192.168.100.6 240 0 200
10 13630577991 192.168.100.7 www.shouhu.com 6960 690 200
11 15043685818 192.168.100.8 www.baidu.com 3659 3538 200
12 15959002129 192.168.100.9 www.baidu.com 1938 180 500
13 13560439638 192.168.100.10 918 4938 200
14 13470253144 192.168.100.11 180 180 200
15 13682846555 192.168.100.12 www.qq.com 1938 2910 200
16 13992314666 192.168.100.13 www.gaga.com 3008 3720 200
17 13509468723 192.168.100.14 www.qinghua.com 7335 110349 404
18 18390173782 192.168.100.15 www.sogou.com 9531 2412 200
19 13975057813 192.168.100.16 www.baidu.com 11058 48243 200
FlowBean
package com.nty.writable; import org.apache.hadoop.io.Writable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; /** * author nty * date time 2018-12-08 13:39 */ //实现Writable接口 public class Flow implements Writable { private Long upflow; //上行流量 private Long downflow; //下行流量 private Long total; //总流量 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 getTotal() { return total; } public void setTotal(Long total) { this.total = total; } /** * 快速赋值 * @param upflow * @param downflow */ public void setFlow(long upflow, long downflow){ this.upflow = upflow; this.downflow = downflow; this.total = upflow + downflow; } //序列化方法 public void write(DataOutput out) throws IOException { out.writeLong(upflow); out.writeLong(downflow); out.writeLong(total); } //反序列化方法,读取数据的顺序和序列化写值得顺序一致 public void readFields(DataInput in) throws IOException { upflow = in.readLong(); downflow = in.readLong(); total = in.readLong(); } @Override public String toString() { return upflow + "\t" + downflow + "\t" + total; } }
Mapper类
package com.nty.writable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; /** * author nty * date time 2018-12-10 16:44 */ //输入的K,V类型为偏移量和本行内容,输出的K,V类型为本行的手机号和Folw对象 public class FlowMapper extends Mapper<LongWritable, Text, Text, Flow> { private Flow flow = new Flow(); private Text phone = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] fields = value.toString().split("\t"); //设置输入的K为手机号 phone.set(fields[1]); //设置Flow,用倒取的原因是因为,有些行数据没有访问地址 flow.setFlow( Long.parseLong(fields[fields.length-3]), Long.parseLong(fields[fields.length-2]) ); //将map后的结果输出给context context.write(phone,flow); } }
Reducer类
package com.nty.writable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /** * author nty * date time 2018-12-10 16:44 */ //Reducer的输入K,V类型为Mapper的输出类型,即Text,Flow,输出K,V类型为手机号和Flow,即Text,Flow public class FlowReducer extends Reducer<Text, Flow, Text, Flow> { private Flow flow = new Flow(); @Override protected void reduce(Text key, Iterable<Flow> values, Context context) throws IOException, InterruptedException { long upflow = 0; long downflow = 0; //遍历values,计算总的下行流量和上行流量 for (Flow flow : values) { upflow += flow.getUpflow(); downflow += flow.getDownflow(); } //对象赋值 flow.setFlow(upflow, downflow); //写出 context.write(key, flow); } }
Driver类
package com.nty.writable; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; 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; /** * author nty * date time 2018-12-10 16:44 */ public class FlowDriver { public static void main(String[] args) throws Exception { //获取配置信息和job Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration); //设置jar类 job.setJarByClass(FlowDriver.class); //设置Mapper和Reducer job.setMapperClass(FlowMapper.class); job.setReducerClass(FlowReducer.class); //设置Mapper的输出类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Flow.class); //设置Reduce的输出类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(Flow.class); //设置文件的输入输出路径 FileInputFormat.setInputPaths(job, new Path("d:\\Hadoop_test")); FileOutputFormat.setOutputPath(job, new Path("d:\\Hadoop_test_output")); //提交 boolean res = job.waitForCompletion(true); System.exit(res ? 0 :1); } }
最终输出结果
13470253144 180 180 360 13509468723 7335 110349 117684 13560439638 918 4938 5856 13590439668 1116 954 2070 13630577991 6960 690 7650 13682846555 1938 2910 4848 13729199489 240 0 240 13736230513 2481 24681 27162 13846544121 264 0 264 13956435636 132 1512 1644 13966251146 240 0 240 13975057813 11058 48243 59301 13992314666 3008 3720 6728 15043685818 3659 3538 7197 15910133277 3156 2936 6092 15959002129 1938 180 2118 18271575951 1527 2106 3633 18390173782 9531 2412 11943 84188413 4116 1432 5548
6.序列化执行过程(源码)
map方法将flow对象write出去,框架接收到flow对象,进行序列化
step into
这是key的序列化方法
再跳出看value的序列化
step into
最后value进入我们自己重写的序列化方法
原文地址:https://www.cnblogs.com/duoduotouhenying/p/10098114.html