Repository navigation
Expand file tree
/
Copy pathReduce1.java
More file actions
69 lines (56 loc) · 1.95 KB
/
Copy pathReduce1.java
File metadata and controls
69 lines (56 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package recommend.step1;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Reduce1 extends Reducer<Text, Text, Text, Text>
{
private Text outKey = new Text();
private Text outValue = new Text();
/**
* @param key 物品
* @param values 用户_分数,用户_分数,用户_分数,用户_分数...
* @param context
* @throws IOException
* @throws InterruptedException
*/
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException
{
String itemId = key.toString();
//<userID,score>
Map<String, Integer> map = new HashMap<String, Integer>();
//统计同一个商品中不同用户的分数 放入map中
for (Text val : values)
{
String userId = val.toString().split("_")[0];
String score = val.toString().split("_")[1];
if (map.get(userId) == null)
map.put(userId, Integer.valueOf(score));
else
{
Integer preScore = map.get(userId);
map.put(userId, Integer.valueOf(score) + preScore);
}
}
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, Integer> entry : map.entrySet())
{
String udrtID = entry.getKey();
String score = String.valueOf(entry.getValue());
stringBuilder.append(udrtID + "_" + score + ",");
}
//去掉行末的","
String line = null;
if (stringBuilder.toString().endsWith(","))
{
line = stringBuilder.substring(0, stringBuilder.length() - 1);
}
if (itemId != null || itemId != "")
{
outKey.set(itemId);
outValue.set(line);
context.write(outKey, outValue);
}
}
}