本关任务:根据手机流量数据,编写 MapReduce 程序来统计出每个手机号码的一年总流量。
时间: 2024-05-06 17:19:42 浏览: 16
思路:
- Mapper 阶段:将每行数据按照手机号码为 key,流量为 value 发射出去。
- Reducer 阶段:将同一个手机号码的所有流量累加起来,输出结果。
Mapper 代码:
```java
public class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
private Text phoneNum = new Text();
private LongWritable flow = new LongWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split("\t");
if (fields.length >= 3) {
phoneNum.set(fields[0]);
flow.set(Long.parseLong(fields[2]));
context.write(phoneNum, flow);
}
}
}
```
Reducer 代码:
```java
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);
}
}
```
完整代码:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class TotalFlow {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "total_flow");
job.setJarByClass(TotalFlow.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
private Text phoneNum = new Text();
private LongWritable flow = new LongWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split("\t");
if (fields.length >= 3) {
phoneNum.set(fields[0]);
flow.set(Long.parseLong(fields[2]));
context.write(phoneNum, flow);
}
}
}
public static 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);
}
}
}
```
运行命令:
```shell
hadoop jar total_flow.jar TotalFlow input output
```
其中,`input` 为输入文件路径,`output` 为输出文件路径。