TA的每日心情 | 开心 昨天 20:49 |
---|
签到天数: 45 天 [LV.5]常住居民I
暂避锋芒
- UID
- 8534
- 云币
- 0
- 金钱
- 223
- 好评
- 0
- 性别
- 保密
- 在线时间
- 16 小时
- 注册时间
- 2024-10-26
|
发表于 2024-11-30 00:15:58
来自手机
|
显示全部楼层
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));
}
}
|
|