BD-exp-2/src/main/java/ProvinceMaxDailyPowerReducer.java
fly6516 2c5af64557 feat: 实现大数据实验2- 添加 Hadoop MapReduce程序,计算不同维度的电力使用情况
- 新增 AnnualIndustrialPowerMapper 和 AnnualIndustrialPowerReducer 类
- 新增 MonthlyResidentialPowerMapper 和 MonthlyResidentialPowerReducer 类
- 实现 Main 类,包含三个 MapReduce 作业的运行逻辑
- 添加单元测试 App 和 AppTest
- 创建项目配置文件,如 pom.xml、.gitignore等
2025-03-03 10:39:39 +08:00

26 lines
814 B
Java

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class ProvinceMaxDailyPowerReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
String maxRecord = null;
double maxValue = Double.MIN_VALUE;
for (Text value : values) {
String[] parts = value.toString().split(":");
double powerUsage = Double.parseDouble(parts[2]);
if (powerUsage > maxValue) {
maxValue = powerUsage;
maxRecord = value.toString();
}
}
if (maxRecord != null) {
context.write(key, new Text(maxRecord));
}
}
}