From e1f0edd5c3fb32ce1fd5436b9653a7ebdcc14edf Mon Sep 17 00:00:00 2001 From: sotech117 Date: Sun, 10 Dec 2023 14:46:20 -0500 Subject: 1st iteration --- WordCount.java | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'WordCount.java') 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> inputs = FileParser.split(text, numSplites); // TODO: instantiate a MapReduce object with correct input, key, value, and output types + MapReduce, 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, String, Long> { @Override public Map compute() { // TODO: implement the Map function for word count - throw new UnsupportedOperationException("WordCount map function not implemented yet."); + Map map = new HashMap<>(); + for (String word : input) { + map.merge(word, 1L, Long::sum); + } + return map; } } - class Reducer + static class Reducer extends mapreduce.Reducer { @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; } } -- cgit v1.2.3-70-g09d2