本关任务:根据公司薪资数据,编写 MapReduce 程序来统计出各部门每月的平均薪资。
时间: 2024-06-12 11:08:28 浏览: 22
根据提供的引用内容,您需要编写一个MapReduce程序来统计公司每个部门每月的平均薪资。首先,您需要上传并运行一个名为"salary.jar"的jar包,使用以下命令:hadoop jar salary.jar first.mainz /input/salary.txt /output/wc2。然后,您可以通过MapReduce的序列化方法来实现该功能。在处理过程中,请确保Reducer的逻辑正确,以避免统计结果错误。根据需求描述,您的任务是编写一个MapReduce程序来计算每个部门每月的平均薪资。
相关问题
本关任务:根据手机流量数据,编写 MapReduce 程序来统计出每个手机号码的一年总流量。
思路:
- 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` 为输出文件路径。
根据手机流量数据,编写 MapReduce 程序来统计出每个手机号码的一年总流量。
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);
}
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)