Repository navigation
Expand file tree
/
Copy pathRecommendFriends.java
More file actions
157 lines (103 loc) · 4.2 KB
/
Copy pathRecommendFriends.java
File metadata and controls
157 lines (103 loc) · 4.2 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package aic.proj.test;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class RecommendFriends {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>
{
private final static Text userId = new Text();
private Text friend = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String line = value.toString();
try
{
JSONObject json = (JSONObject) new JSONParser().parse(line);
Iterator iter = json.keySet().iterator();
String userIdPair = (String) iter.next();
if(Integer.parseInt(json.get(userIdPair).toString()) > 100)
{
String[] users = userIdPair.split(",");
userId.set(users[0]);
friend.set(users[1]);
output.collect(userId, friend);
output.collect(friend, userId);
}
}
catch(ParseException e)
{
e.printStackTrace();
}
}
}
public static class Combine extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text friends = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String friendList = "{\""+key+"\":[";
while (values.hasNext())
{
String friend = values.next().toString();
friendList += "\""+friend+"\",";
}
friendList = friendList.substring(0, friendList.length()-1);
friendList += "]}";
friends.set(friendList);
output.collect(key, friends);
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text friendList = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String friendIds = "[";
while (values.hasNext())
{
try
{
String friends = values.next().toString();
JSONObject json = (JSONObject) new JSONParser().parse(friends);
Iterator iter = json.keySet().iterator();
String userId = (String) iter.next();
JSONArray jsonFriendList = (JSONArray) json.get(userId);
for(int i=0;i<jsonFriendList.size();i++)
{
String friend = jsonFriendList.get(i).toString();
friendIds += "\""+friend+"\",";
}
}
catch(ParseException e)
{
e.printStackTrace();
}
}
friendIds = friendIds.substring(0, friendIds.length()-1);
friendIds += "]";
friendList.set("?"+friendIds);
key.set(key.toString()+":");
output.collect(key, friendList);
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(RecommendFriends.class);
conf.setJobName("recommendfriends");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(Text.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Combine.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}