hadoop序列化实现案例代码
作者:chen18677338530
序列化想必大家都很熟悉了,对象在进行网络传输过程中,需要序列化之后才能传输到客户端,或者客户端的数据序列化之后送达到服务端,本文将为大家介绍Hadoop如何实现序列化,需要的可以参考一下
Hadoop序列化特点
- 紧凑:高效实用存储空间
- 快速:读写数据额外开销小
- 可扩展:随着通信协议的升级而可以升级
- 互操作:支持多种语言的交互
自定义Bean对象实现序列化
- 必须实现Writable接口
- 反序列化时,需要反射调用无参构造函数
- 如果需要将自定义的bean放在key中传输,则还需要实现Comparable接口
案例
package com.chen.phoneproject; 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 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 FlowBean() { super(); } public FlowBean(long upFlow, long downFlow) { super(); this.upFlow = upFlow; this.downFlow = downFlow; } public FlowBean(long upFlow, long downFlow, long sumFlow) { super(); this.upFlow = upFlow; this.downFlow = downFlow; this.sumFlow = sumFlow; } @Override public void write(DataOutput dataOutput) throws IOException { dataOutput.writeLong(upFlow); dataOutput.writeLong(downFlow); dataOutput.writeLong(sumFlow); } @Override public void readFields(DataInput dataInput) throws IOException { this.upFlow = dataInput.readLong(); this.downFlow = dataInput.readLong(); this.sumFlow = dataInput.readLong(); } @Override public String toString() { return "FlowBean{" + "upFlow=" + upFlow + ", downFlow=" + downFlow + ", sumFlow=" + sumFlow + '}'; } }
package com.chen.phoneproject; import lombok.extern.slf4j.Slf4j; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; @Slf4j public class FlowCountMapper extends Mapper<LongWritable, Text,Text,FlowBean> { Text k = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { log.info("---mapper---"+"key:"+key+",value:"+value); String line = value.toString(); String[] fields = line.split("\t"); String phoneNum = fields[1]; long upFlow = Long.parseLong(fields[3]); long downFlow = Long.parseLong(fields[4]); k.set(phoneNum); FlowBean bean = new FlowBean(upFlow,downFlow); context.write(k,bean); } }
package com.chen.phoneproject; import lombok.extern.slf4j.Slf4j; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; @Slf4j public class FlowCountReducer extends Reducer<Text,FlowBean,Text,FlowBean> { @Override protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException { log.info("---reduce---"+"key:"+key+",value:"+values); long sum_upFlow = 0; long sum_downFlow = 0; for (FlowBean flowBean:values){ sum_upFlow += flowBean.getUpFlow(); sum_downFlow += flowBean.getDownFlow(); } FlowBean result = new FlowBean(sum_upFlow,sum_downFlow,sum_downFlow + sum_upFlow); context.write(key,result); } }
package com.chen.phoneproject; import com.chen.mapreduce.WordcountDriver; import com.chen.mapreduce.WordcountMapper; import com.chen.mapreduce.WordcountReducer; 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; public class FlowsumDriver { public static void main(String[] args) throws Exception { Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration); job.setJarByClass(FlowsumDriver.class); job.setMapperClass(FlowCountMapper.class); job.setReducerClass(FlowCountReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(FlowBean.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(FlowBean.class); FileInputFormat.setInputPaths(job,new Path(args[0])); FileOutputFormat.setOutputPath(job,new Path(args[1])); boolean result = job.waitForCompletion(true); System.exit(result ? 0 : 1); } }
总结
到此这篇关于hadoop序列化实现的文章就介绍到这了,更多相关Hadoop序列化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!