大数据hadoop3.1.3——Hadoop序列化以及案例操作
程序员文章站
2022-04-28 18:11:11
...
1、序列化概述
2、自定义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;
}
3 序列化案例实操
2.需求分析
3.编写MapReduce程序
(1)编写流量统计的Bean对象
package com.caron.mr.writable;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* @author Caron
* @create 2020-04-16-20:11
* @Description
* @Version
*/
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 void setSumFlow(){
this.sumFlow = this.upFlow + this.downFlow;
}
/**
* 重写toString方法
* @return
*/
@Override
public String toString() {
return this.upFlow + "/t" + this.downFlow + "/t" + this.sumFlow;
}
/**
* 提供无参构造器,反序列化会反射调用
*/
public FlowBean() { }
/**
* 序列化方法
* @param dataOutput
* @throws IOException
*/
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeLong(upFlow);
dataOutput.writeLong(downFlow);
dataOutput.writeLong(sumFlow);
}
/**
* 反序列化方法
* @param dataInput
* @throws IOException
*/
public void readFields(DataInput dataInput) throws IOException {
this.upFlow = dataInput.readLong();
this.downFlow = dataInput.readLong();
this.sumFlow = dataInput.readLong();
}
}
(2)编写Mapper类
package com.caron.mr.writable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @author Caron
* @create 2020-04-16-20:11
* @Description
* @Version
*/
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[] splits = line.split("\t");
//封装key
outK.set(splits[1]);
//封装value
outV.setUpFlow(Long.parseLong(splits[splits.length-3]));
outV.setDownFlow(Long.parseLong(splits[splits.length-2]));
outV.getSumFlow();
//写出
context.write(outK,outV);
}
}
(3)编写Reducer类
package com.caron.mr.writable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @author Caron
* @create 2020-04-16-20:11
* @Description
* @Version
*/
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 totalUpFlow = 0;
//总下行
long totalDownFlow = 0;
//总流量
long totalSumFlow = 0;
//处理一个手机号的
for (FlowBean flowBean :
values) {
totalUpFlow += flowBean.getUpFlow();
totalDownFlow += flowBean.getDownFlow();
totalSumFlow += flowBean.getSumFlow();
}
//封装
outV.setUpFlow(totalUpFlow);
outV.setDownFlow(totalDownFlow);
outV.setSumFlow(totalSumFlow);
//写出
context.write(key,outV);
}
}
(4)编写Driver驱动类
package com.caron.mr.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;
/**
* @author Caron
* @create 2020-04-16-20:11
* @Description
* @Version
*/
public class FlowDrriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(FlowDrriver.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("F:/input/in.txt"));
FileOutputFormat.setOutputPath(job,new Path("F:/output"));
job.waitForCompletion(true);
}
}