TA的每日心情 | 开心 2 小时前 |
---|
签到天数: 34 天 [LV.5]常住居民I
浅入佳境
- UID
- 8534
- 云币
- 0
- 金钱
- 169
- 好评
- 0
- 性别
- 保密
- 在线时间
- 11 小时
- 注册时间
- 2024-10-26
|
发表于 2 小时前
来自手机
|
显示全部楼层
IP:贵州黔南布依族苗族自治州
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
// Mapper 类
public static class PurchaseCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] parts = line.split("\t"); // 根据实际数据的分隔符进行拆分
String goods = parts[1]; // 假设商品名称在第二列
context.write(new Text(goods), new IntWritable(1));
}
}
// Reducer 类
public static class PurchaseCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
context.write(key, new IntWritable(sum));
}
}
|
|