根据手机流量数据,编写 MapReduce 程序来统计出每个手机号码的一年总流量。
时间: 2024-05-09 15:19:30 浏览: 211
MapReduce统计手机上网流量
5星 · 资源好评率100%
Mapper阶段:
输入:每行数据包括手机号码、日期、上行流量、下行流量,以制表符分隔。
输出:键为手机号码,值为一年内的总流量,以制表符分隔。
```
public class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
private Text phoneNum = new Text();
private LongWritable totalFlow = new LongWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().split("\t");
String phoneNumStr = words[0];
long upFlow = Long.parseLong(words[2]);
long downFlow = Long.parseLong(words[3]);
phoneNum.set(phoneNumStr);
totalFlow.set(upFlow + downFlow);
context.write(phoneNum, totalFlow);
}
}
```
Reducer阶段:
输入:每个手机号码的流量总和,以键值对的形式传递。
输出:每个手机号码的一年总流量,以制表符分隔。
```
public class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
private LongWritable totalFlow = new LongWritable();
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long sum = 0;
for (LongWritable value : values) {
sum += value.get();
}
totalFlow.set(sum);
context.write(key, totalFlow);
}
}
```
阅读全文