-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopKWordCount.java
146 lines (124 loc) · 4.84 KB
/
TopKWordCount.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
import java.io.IOException;
import java.util.*;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 TopKWordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
static <K,V extends Comparable<? super V>>
SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
new Comparator<Map.Entry<K,V>>() {
@Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
int res = e2.getValue().compareTo(e1.getValue());
return res != 0 ? res : 1;
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
// Create your 2nd mapper here
public static class IdMapper
extends Mapper<Object, Text, Text, Text>{
private Text word_freq = new Text();
private Text k = new Text("-");
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
Scanner san = new Scanner(value.toString());
while (san.hasNextLine()) {
word_freq.set(san.nextLine());
context.write(k, word_freq);
}
}
}
// Create your 2nd reducer here
public static class TopKMaxReducer
extends Reducer<Text,Text,Text,Text> {
private Text result = new Text();
private String topk_word;
Map<String, Integer> map = new TreeMap<String, Integer>();
SortedSet<Map.Entry<String, Integer>> mapSorted = new TreeSet<Map.Entry<String, Integer>>();
int count = 0;
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
int kval = Integer.parseInt(conf.get("numResults"));
for(Text val: values) {
Scanner san = new Scanner(val.toString());
map.put(san.next(), san.nextInt());
}
mapSorted = entriesSortedByValues(map);
for (Map.Entry<String,Integer> element: mapSorted){
String word = element.getKey();
String freq = Integer.toString(element.getValue());
if(count < kval){
topk_word = word + "\t" + freq;
result.set(topk_word);
context.write(key, result);
count += 1;
}
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(TopKWordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
Configuration conf2 = new Configuration();
conf2.set("numResults", args[3]);
Job job2 = Job.getInstance(conf2, "word count");
// Insert your Code Here
job2.setJarByClass(TopKWordCount.class);
job2.setMapperClass(IdMapper.class);
job2.setCombinerClass(TopKMaxReducer.class);
job2.setReducerClass(TopKMaxReducer.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job2, new Path(args[1]));
FileOutputFormat.setOutputPath(job2, new Path(args[2]));
System.exit(job2.waitForCompletion(true) ? 0 : 1);
}
}