aboutsummaryrefslogtreecommitdiff
path: root/WordCount.java
diff options
context:
space:
mode:
Diffstat (limited to 'WordCount.java')
-rw-r--r--WordCount.java24
1 files changed, 18 insertions, 6 deletions
diff --git a/WordCount.java b/WordCount.java
index 3c224d4..feb1b3c 100644
--- a/WordCount.java
+++ b/WordCount.java
@@ -1,3 +1,4 @@
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -20,32 +21,43 @@ public class WordCount {
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
-
- throw new UnsupportedOperationException("WordCount.run() not implemented yet.");
+ return mapReduce.call();
}
- class Mapper
+ static class Mapper
extends mapreduce.Mapper<List<String>, String, Long> {
@Override
public Map<String, Long> compute() {
// TODO: implement the Map function for word count
- throw new UnsupportedOperationException("WordCount map function not implemented yet.");
+ Map<String, Long> map = new HashMap<>();
+ for (String word : input) {
+ map.merge(word, 1L, Long::sum);
+ }
+ return map;
}
}
- class Reducer
+ static class Reducer
extends mapreduce.Reducer<String, Long, Long> {
@Override
public Long compute() {
// TODO: implement the Reduce function for word count
- throw new UnsupportedOperationException("WordCount reduce function not implemented yet.");
+ long count = 0;
+ for (Long value : valueList) {
+ count += value;
+ }
+ return count;
}
}