MapReduce之Hadoop的序列化
文章目录
1. 序列化概述
(1)什么是序列化
序列化就是把内存中的对象,转换成字节序列 (或其他数据传输协议)以便于存储到磁盘(持久化)和网络传输。
反序列化就是将收到字节序列(或其他数据传输协议)或者是磁盘的持久化数据,转换成内存中的对象。
(2)为什么要序列化
一般来说,“活的”对象只生存在内存里,关机断电就没有了。而且“活的”对象只能由本地的进程使用,不能被发送到网络上的另外一台计算机。 然而序列化可以存储“活的”对象,可以将“活的”对象发送到远程计算机。
(3)为什么不用 Java 的序列化
Java 的序列化是一个重量级序列化框架(Serializable),一个对象被序列化后,会附带很多额外的信息(各种校验信息, Header,继承体系等),不便于在网络中高效传输。所以,Hadoop 自己开发了一套序列化机制(Writable)。
(4)Hadoop 序列化特点:
紧凑 :高效使用存储空间。
快速:读写数据的额外开销小。
互操作:支持多语言的交互
2. 自定义 bean 对象实现序列化接口(Writable)
在企业开发中往往常用的基本序列化类型不能满足所有需求, 比如在 Hadoop 框架内部传递一个 bean 对象,那么该对象就需要实现序列化接口。
具体实现 bean 对象序列化步骤如下 7 步。
(1)必须实现 Writable 接口
(2)反序列化时,需要反射调用空参构造函数,所以必须有空参构造
1 2 3 public FlowBean () { super (); }
(3)重写序列化方法
1 2 3 4 5 6 @Override public void write (DataOutput out) throws IOException { out.writeLong(upFlow); out.writeLong(downFlow); out.writeLong(sumFlow); }
(4)重写反序列化方法
1 2 3 4 5 6 @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 必须能排序。详见后面排序案例。
1 2 3 4 5 @Override public int compareTo (FlowBean o) { return this .sumFlow > o.getSumFlow() ? -1 : 1 ; }
3. 序列化案例实操
(1)需求
统计每一个手机号耗费的总上行流量、总下行流量、总流量
① 输入数据phone_data.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 13736230513 192.196.100.1 www.atguigu.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.atguigu.com 1527 2106 200 6 84188413 192.168.100.3 www.atguigu.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.atguigu.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 20 13768778790 192.168.100.17 120 120 200 21 13568436656 192.168.100.18 www.alibaba.com 2481 24681 200 22 13568436656 192.168.100.19 1116 954 200
② 输入数据格式:
③ 期望输出数据格式
(2)需求分析
(3)编写 MapReduce 程序
① 编写流量统计的 Bean 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package com.layne.mapreduce.writable;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; } @Override public void write (DataOutput out) throws IOException { out.writeLong(upFlow); out.writeLong(downFlow); out.writeLong(sumFlow); } @Override public void readFields (DataInput in) throws IOException { this .upFlow = in.readLong(); this .downFlow = in.readLong(); this .sumFlow = in.readLong(); } @Override public String toString () { return upFlow + "\t" + downFlow + "\t" + sumFlow; } }
② 编写 Mapper 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package com.layne.mapreduce.writable;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(); @Override protected 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); } }
③ 编写 Reducer 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.layne.mapreduce.writable;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(); @Override protected void reduce (Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException { long totalUp = 0 ; long totaldown = 0 ; for (FlowBean value : values) { totalUp += value.getUpFlow(); totaldown += value.getDownFlow(); } outV.setUpFlow(totalUp); outV.setDownFlow(totaldown); outV.setSumFlow(); context.write(key, outV); } }
④ 编写 Driver 驱动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.layne.mapreduce.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;import java.io.IOException;public class FlowDriver { public static void main (String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(FlowDriver.class ) ; job.setMapperClass(FlowMapper.class ) ; job.setReducerClass(FlowReducer.class ) ; job.setMapOutputKeyClass(Text.class ) ; job.setMapOutputValueClass(FlowBean.class ) ; job.setOutputKeyClass(Text.class ) ; job.setOutputValueClass(FlowBean.class ) ; FileInputFormat.setInputPaths(job, new Path("D:\\test\\inputflow" )); FileOutputFormat.setOutputPath(job, new Path("D:\\test\\outputflow" )); boolean result = job.waitForCompletion(true ); System.exit(result ? 0 : 1 ); } }
⑤ 输出结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 13470253144 180 180 360 13509468723 7335 110349 117684 13560439638 918 4938 5856 13568436656 3597 25635 29232 13590439668 1116 954 2070 13630577991 6960 690 7650 13682846555 1938 2910 4848 13729199489 240 0 240 13736230513 2481 24681 27162 13768778790 120 120 240 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