-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recommendation.java
162 lines (126 loc) · 5.79 KB
/
Recommendation.java
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
// by Aashish Dugar
package dugar.recommendation_System;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class Recommendation extends Configured implements Tool {
@SuppressWarnings("deprecation")
@Override
public int run(String[] args) throws Exception {
System.out.println(Arrays.toString(args));
Job job = new Job(getConf(), "Recommendation");
job.setJarByClass(Recommendation.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
return 0;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(Arrays.toString(args));
int res = ToolRunner.run(new Configuration(), new Recommendation(), args);
System.exit(res);
}
}
class Map extends Mapper<LongWritable, Text, Text, Text> {
private Text user = new Text();
private Text user1 = new Text();
private Text user2 = new Text();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
int i,j;
String s[] = value.toString().split("\\t");
String val[];
if (s.length > 1) {
val = s[1].split(",");
}
else
val = null;
user.set(s[0]);
if(val != null){
for(i=0;i<val.length-1;i++){
user2.set(val[i]+"#-3000");
context.write(user, user2);
for(j=i+1;j<val.length;j++){
user1.set(val[i]);
user2.set(val[j]+"#1");
context.write(user1, user2);
user1.set(val[j]);
user2.set(val[i]+"#1");
context.write(user1, user2);
}
}
user2.set(val[i]+"#-3000");
context.write(user, user2);
}
}
}
class Reduce extends Reducer<Text, Text, IntWritable, Text> {
class ValueComparator implements Comparator<String> {
HashMap<String, Integer> base;
public ValueComparator(HashMap<String, Integer> base) {
this.base = base;
}
// To handle inconsistencies
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1; // 0 for merging keys
}
}
}
@Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
HashMap<String,Integer> map1= new HashMap<String,Integer>();
ValueComparator cmp = new ValueComparator(map1);
TreeMap<String,Integer>map2 = new TreeMap<String,Integer>(cmp);
while(values.iterator().hasNext()) {
String s[] = values.iterator().next().toString().split("#");
if(!map1.containsKey(s[0]))
map1.put(s[0], 0);
map1.put(s[0],map1.get(s[0])+Integer.parseInt(s[1]));
}
map2.putAll(map1);
ArrayList<String> list = new ArrayList<String>();
int i=0;
for(Entry<String, Integer> e: map2.entrySet()){
if(i<10 && e.getValue()>0)
list.add(e.getKey());
;
i++;
}
String out = list.toString();
context.write(new IntWritable(Integer.parseInt(key.toString())), new Text(out));
}
}