Repository navigation
Expand file tree
/
Copy pathCommonMusic.java
More file actions
155 lines (103 loc) · 4.09 KB
/
Copy pathCommonMusic.java
File metadata and controls
155 lines (103 loc) · 4.09 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
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 CommonMusic {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>
{
private final static Text musicId = new Text();
private Text userId = 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 user = (String) iter.next();
userId.set(user);
JSONArray jsonMusicList = (JSONArray) json.get(user);
for(int i=0;i<jsonMusicList.size();i++)
{
String music = jsonMusicList.get(i).toString();
musicId.set(music);
output.collect(musicId, userId);
}
}
catch(ParseException e)
{
e.printStackTrace();
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text userList = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String users = "[";
while (values.hasNext())
{
try
{
String userIds = values.next().toString();
JSONObject json = (JSONObject) new JSONParser().parse(userIds);
Iterator iter = json.keySet().iterator();
String musicId = (String) iter.next();
JSONArray jsonUserList = (JSONArray) json.get(musicId);
for(int i=0;i<jsonUserList.size();i++)
{
String user = jsonUserList.get(i).toString();
users += "\""+user+"\",";
}
}
catch(ParseException e)
{
e.printStackTrace();
}
}
users = users.substring(0, users.length()-1);
users += "]";
userList.set("?"+users);
key.set(key.toString()+":");
output.collect(key, userList);
}
}
public static class Combine extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text userList = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String users = "{\""+key+"\":[";
while (values.hasNext())
{
String user = values.next().toString();
users += "\""+user+"\",";
}
users = users.substring(0, users.length()-1);
users += "]}";
userList.set(users);
output.collect(key, userList);
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(CommonMusic.class);
conf.setJobName("commonmusic");
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);
}
}