diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-12-10 14:46:20 -0500 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-12-10 14:46:20 -0500 |
commit | e1f0edd5c3fb32ce1fd5436b9653a7ebdcc14edf (patch) | |
tree | dfa869e0edc65bad73c842ee0e264053d39b266d /WordCount.java | |
parent | cb491e82b5ce3dcb7e3c41973a46cb7dcbaa9008 (diff) |
1st iteration
Diffstat (limited to 'WordCount.java')
-rw-r--r-- | WordCount.java | 24 |
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; } } |