需求分析
统计每一个手机号耗费的总上传流量、下载流量、总流量
数据准备
输入数据格式:
要求输出数据格式:
思路分析
Map阶段
(1)读取一行数据,切分字段
(2)抽取手机号、上行流量、下行流量
(3)以手机号为key,bean对象为value输出,即context.write(手机号,bean);
Reduce阶段
(1)累加上行流量和下行流量得到总流量。
(2)实现自定义的bean来封装流量信息,并将bean作为map输出的key来传输
(3)MR程序在处理数据的过程中会对数据排序(map输出的kv对传输到reduce之前,会排序),排序的依据是map输出的key
代码编写
自定义序列化FlowBean
package flowwb;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class FlowBean implements Writable {
//alt + insert 自动生成get/set()
//上传流量,用来作为排序依据
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 sumupflow) {
this.sumflow = sumupflow;
}
//空参构造器,满足自定义序列化需要
public FlowBean(){
}
//自定义构造器
public FlowBean(long upflow,long downflow){
this.upflow=upflow;
this.downflow=downflow;
this.sumflow=upflow+downflow;
}
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeLong(this.upflow);
dataOutput.writeLong(this.downflow);
dataOutput.writeLong(this.sumflow);
}
public void readFields(DataInput dataInput) throws IOException {
this.upflow = dataInput.readLong();
this.downflow = dataInput.readLong();
this.sumflow = dataInput.readLong();
}
public String toString(){
return this.upflow+"\t"+this.downflow+"\t"+this.sumflow;
}
}
Mapper
package flowwb;
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> {
//ctrl+o 重写继承类中某方法
//alt+insert也可以
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//获取数据
String line = value.toString();
//切分数据
String[] fields = line.split("\t");
int n=fields.length;
//封装数据
FlowBean flowBean = new FlowBean(Long.parseLong(fields[n-3]),Long.parseLong(fields[n-2]));
//用手机号码作reduce的key
context.write(new Text(fields[1]),flowBean);
}
}
Reducer
package flowwb;
import org.apache.hadoop.io.NullWritable;
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> {
//传过来的数据已经按照key值去重了
@Override
protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
//定义总上传流量和下载流量
long sumupflow=0;
long sumdownflow=0;
//遍历每个key(电话号码)的流量数据,计算出总流量
for(FlowBean f:values){
sumupflow+=f.getUpflow();
sumdownflow+=f.getDownflow();
}
FlowBean flowBean = new FlowBean(sumupflow, sumdownflow);
context.write(key,flowBean);
}
}
Driver
package flowwb;
import LogClean.CleanDriver;
import LogClean.CleanMapper;
import Partition.MyPartition;
import Partition.PartitionDriver;
import Partition.PartitionMapper;
import Partition.PartitionReduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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 = Job.getInstance(conf);
//配置job的信息
job.setJarByClass(FlowDriver.class);
//指定自定义的mapper、mapper的数据类型到job中
job.setMapperClass(FlowMapper.class);
job.setOutputKeyClass(Text.class);
job.setMapOutputValueClass(FlowBean.class);
//指定自定义的reduce以及reduce的输出数据类型,总输出类型
job.setReducerClass(FlowReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FlowBean.class);
//配置输入数据的路径,这一部分演示的在本地运行
FileInputFormat.setInputPaths(job,new Path("D:\\BigdataTest\\PhoneData\\phone_data.txt"));
//配置输出数据的路径
FileOutputFormat.setOutputPath(job,new Path("D:\\BigdataTest\\PhoneData\\FlowWritable"));
/*//配置输入数据的路径,这一部分是打包上传到集群运行
FileInputFormat.setInputPaths(job,new Path(args[0]));
//配置输出数据的路径
FileOutputFormat.setOutputPath(job,new Path(args[1]));*/
//提交任务
job.waitForCompletion(true);
}
}