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
|
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mapreduce.MapReduce;
/**
*
* @author mph
*/
public class WordCount {
int numSplites;
public WordCount(int numSplites) {
this.numSplites = numSplites;
}
public Map<String, Long> run(String filename) {
List<String> text = FileParser.parse(filename);
List<List<String>> inputs = FileParser.split(text, numSplites);
// TODO: instantiate a MapReduce object with correct input, key, value, and output types
MapReduce<List<String>, String, Long, Long> mapReduce = new MapReduce<>();
// TODO: set the mapper and reducer suppliers, and set the inputs
mapReduce.setMapperSupplier(Mapper::new);
mapReduce.setReducerSupplier(Reducer::new);
mapReduce.setInput(inputs);
// TODO: execute the MapReduce object and return the result
return mapReduce.call();
}
static class Mapper
extends mapreduce.Mapper<List<String>, String, Long> {
@Override
public Map<String, Long> compute() {
// TODO: implement the Map function for word count
Map<String, Long> map = new HashMap<>();
for (String word : input) {
map.merge(word, 1L, Long::sum);
}
return map;
}
}
static class Reducer
extends mapreduce.Reducer<String, Long, Long> {
@Override
public Long compute() {
// TODO: implement the Reduce function for word count
long count = 0;
for (Long value : valueList) {
count += value;
}
return count;
}
}
}
|