-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopNAveragePageViews.java
143 lines (115 loc) · 5.06 KB
/
TopNAveragePageViews.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
/*
* Get the Top N articles based on Average Page Views per day
*/
package wiki.wiki;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
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.output.FileOutputFormat;
public class TopNAveragePageViews {
public static class TopNAveragePageViewsMap extends Mapper<LongWritable, Text, Text, LongWritable> {
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String delims = ",";
String[] wikiData = StringUtils.split(value.toString(), delims);
Text article = new Text(wikiData[0]);
LongWritable views = new LongWritable(Long.parseLong(wikiData[2]));
context.write(article, views);
}
@Override
protected void setup(Context context) throws IOException, InterruptedException {
}
}
/**
* The reducer retrieves every word and puts it into a Map: if the word already exists in the
* map, increments its value, otherwise sets it to 1.
*/
public static class TopNAveragePageViewsReduce extends Reducer<Text, LongWritable, Text, DoubleWritable> {
private Map<Text, DoubleWritable> countMap = new HashMap<>();
@Override
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
Long views = (long) 0;
Long count = (long) 0;
for (LongWritable t : values) {
views += t.get();
count++;
}
Double average = (double) views/ (double)count;
// puts the number of occurrences of this word into the map.
// We need to create another Text object because the Text instance
// we receive is the same for all the words
countMap.put(new Text(key), new DoubleWritable(average));
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
Map<Text,DoubleWritable> sortedMap = sortByValues(countMap);
int counter = 0;
for (Text key : sortedMap.keySet()) {
if (counter++ == 10) {
break;
}
context.write(key,sortedMap.get(key));
}
}
}
//This should not be used because, this is not associative and commutative
public static class TopNAveragePageViewsCombiner extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
Long views = (long) 0;
for (LongWritable t : values) {
views += t.get();
}
context.write(key, new LongWritable(views));
}
}
/*
* sorts the map by values. Taken from:
* http://javarevisited.blogspot.it/2012/12/how-to-sort-hashmap-java-by-key-and-value.html
*/
private static <K extends Comparable, V extends Comparable> Map<K, V> sortByValues(Map<K, V> map) {
List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
//LinkedHashMap will keep the keys in the order they are inserted
//which is currently sorted on natural ordering
Map<K, V> sortedMap = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJobName("Top N");
job.setJarByClass(TopNPageViews.class);
job.setMapperClass(TopNAveragePageViewsMap.class);
//job.setCombinerClass(TopNAveragePageViewsCombiner.class); //because average is not associative/commutative
job.setReducerClass(TopNAveragePageViewsReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, new Path("/Users/anushakaranam/Downloads/wiki/wikicounts"));
FileOutputFormat.setOutputPath(job, new Path("TopNAveragePageViews"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}