/
VoteCount.java
134 lines (108 loc) · 4.13 KB
/
VoteCount.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
/**
* @author Sharayu
* Calculate votes in unequal democracy based on worth
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
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 VoteCount {
public static class MergeFilesMapper extends Mapper<Object, Text, Text, Text> {
private Text word = new Text();
private Text valWord = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("isitlinewise: " + value.toString());
StringTokenizer itr = new StringTokenizer(value.toString());
String voter = itr.nextToken();
String voterval = itr.nextToken().toUpperCase();
word.set(voter);
valWord.set(voterval);
context.write(word, valWord);
}
}
public static class CalculateVotesMapper extends Mapper<Object, Text, Text, IntWritable> {
private Text word = new Text();
private IntWritable wordWorth = new IntWritable();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("isitlinewise2: " + value.toString());
StringTokenizer itr = new StringTokenizer(value.toString());
System.out.println(value);
// 1st token is the key, you don't want to process it
itr.nextToken();
int worth = 0;
List<String> valList = new ArrayList<String>();
System.out.println("map1----" + key);
while (itr.hasMoreTokens()) {
String val = itr.nextToken();
System.out.println("map1 vals1: " + val.toString());
try {
worth = Integer.parseInt(val.toString());
System.out.println("map1 worth: " + worth);
} catch (NumberFormatException e) {
valList.add(val.toString());
}
}
for (String val : valList) {
System.out.println("map1 vals2: " + val.toString() + " " + worth);
word.set(val);
wordWorth.set(worth);
context.write(word, wordWorth);
}
}
}
public static class MergedFilesReducer extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
String avals = "";
for (Text val : values) {
avals = avals + " " + val.toString();
}
result.set(avals);
context.write(key, result);
}
}
public static class CalculateVotesReducer 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 votesum = 0;
for (IntWritable val : values) {
votesum += val.get();
}
result.set(votesum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job1 = new Job(conf, "combine votes");
job1.setJarByClass(VoteCount.class);
job1.setMapperClass(MergeFilesMapper.class);
job1.setCombinerClass(MergedFilesReducer.class);
job1.setReducerClass(MergedFilesReducer.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job1, new Path(args[0]));
FileOutputFormat.setOutputPath(job1, new Path(args[1] + "-tmp"));
job1.waitForCompletion(true);
Job job2 = new Job(conf, "votes count");
job2.setJarByClass(VoteCount.class);
job2.setMapperClass(CalculateVotesMapper.class);
job2.setCombinerClass(CalculateVotesReducer.class);
job2.setReducerClass(CalculateVotesReducer.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job2, new Path(args[1] + "-tmp"));
FileOutputFormat.setOutputPath(job2, new Path(args[1]));
System.exit(job2.waitForCompletion(true) ? 0 : 1);
}
}