Repository navigation
Expand file tree
/
Copy pathCommonMusic2.java
More file actions
168 lines (112 loc) · 4.58 KB
/
Copy pathCommonMusic2.java
File metadata and controls
168 lines (112 loc) · 4.58 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
158
159
160
161
162
163
164
165
166
167
168
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 CommonMusic2 {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>
{
private final static Text userPair = new Text();
private Text musicId = 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 music = (String) iter.next();
musicId.set(music);
JSONArray jsonUserList = (JSONArray) json.get(music);
for(int i=0;i<jsonUserList.size();i++)
{
for(int j=0;j<jsonUserList.size() && j!=i;j++)
{
String user1 = jsonUserList.get(i).toString();
String user2 = jsonUserList.get(j).toString();
String pair="";
if(user1.compareTo(user2) <= 0)
pair = user1+","+user2;
else
pair = user2+","+user1;
userPair.set(pair);
output.collect(userPair, musicId);
}
}
}
catch(ParseException e)
{
e.printStackTrace();
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text musicList = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String musicIds = "[";
while (values.hasNext())
{
try
{
String musics = values.next().toString();
JSONObject json = (JSONObject) new JSONParser().parse(musics);
Iterator iter = json.keySet().iterator();
String userIdPair = (String) iter.next();
JSONArray jsonMusicList = (JSONArray) json.get(userIdPair);
for(int i=0;i<jsonMusicList.size();i++)
{
String music = jsonMusicList.get(i).toString();
musicIds += "\""+music+"\",";
}
}
catch(ParseException e)
{
e.printStackTrace();
}
}
musicIds = musicIds.substring(0, musicIds.length()-1);
musicIds += "]";
musicList.set("?"+musicIds);
key.set(key.toString()+":");
output.collect(key, musicList);
}
}
public static class Combine extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
private Text musicList = new Text();
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String musicIds = "{\""+key+"\":[";
while (values.hasNext())
{
String music = values.next().toString();
musicIds += "\""+music+"\",";
}
musicIds = musicIds.substring(0, musicIds.length()-1);
musicIds += "]}";
musicList.set(musicIds);
output.collect(key, musicList);
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(CommonMusic2.class);
conf.setJobName("commonmusic2");
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);
}
}