Hadoop MapReduce WordCount案例 Mapper类的解析
wordcount案例里面
输入的key是LongWritable-偏移量
输入的value是Text–>每一行数据
19-Hadoop MapReduce WordCount案例 Reducer类的解析:
20-Hadoop MapReduce WordCount案例 Debug使用介绍
一行一行调试
进入方法的调用
强制进入到方法里面
跳出方法的调用
直接跳到下个断点的位置
统计每一个手机号耗费的总上行流量、下行流量、总流量
(1)编写流量统计的Bean对象
package com.atguigu.phonenumber1;
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 set(long upFLow,long downFlow){
this.upFlow=upFLow;
this.downFLow=downFlow;
this.sumFlow = upFlow+downFLow;
}
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 write(DataOutput out) throws IOException {
out.writeLong(upFlow);
out.writeLong(downFLow);
out.writeLong(sumFlow);
}
public void readFields(DataInput in) throws IOException {
upFlow = in.readLong();
downFLow = in.readLong();
sumFlow = in.readLong();
}
@Override
public String toString() {
return upFlow+"\t"+downFLow+"\t"+sumFlow;
}
}
(2)编写Mapper类
package com.atguigu.phonenumber1;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
//Mapper的输出key:手机号 value:FlowBean对象
public class PhoneMapper extends Mapper<LongWritable, Text,Text,FlowBean> {
Text outKey=new Text();
FlowBean outValue=new FlowBean();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String phoneInfo = value.toString();
String[] split = phoneInfo.split("\t");
String phone=split[1];
outKey.set(phone);
long downFlow = Long.parseLong(split[split.length - 2]);
long upFlow = Long.parseLong(split[split.length - 3]);
outValue.set(upFlow,downFlow);
context.write(outKey,outValue);
}
}
(3)编写Reducer类
package com.atguigu.phonenumber1;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class PhoneReducer extends Reducer<Text,FlowBean,Text,FlowBean> {
FlowBean outValue=new FlowBean();
@Override
protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
long sumUpFlow=0;
long sumDownFlow=0;
for (FlowBean flowBean : values) {
sumUpFlow+=flowBean.getUpFlow();
sumDownFlow+=flowBean.getDownFLow();
}
outValue.set(sumUpFlow,sumDownFlow);
context.write(key,outValue);
}
}
(4)编写Driver驱动类
package com.atguigu.phonenumber1;
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 PhoneDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(PhoneDriver.class);
job.setMapperClass(PhoneMapper.class);
job.setReducerClass(PhoneReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(FlowBean.class);
job.setOutputKeyClass(Text.class);
job.setOutputKeyClass(FlowBean.class);
FileInputFormat.setInputPaths(job,new Path("E:\\io\\input\\phone_data.txt"));
FileOutputFormat.setOutputPath(job,new Path("E:\\io\\output"));
job.waitForCompletion(true);
}
}