Skip to content

Commit

Permalink
Alternative versions of word count mappers that are closer to pseudo-…
Browse files Browse the repository at this point in the history
…code.
  • Loading branch information
lintool committed Jan 10, 2017
1 parent d8b1198 commit 0bdde0a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/scala/io/bespin/scala/mapreduce/wordcount/WordCount.scala
Expand Up @@ -53,6 +53,16 @@ object WordCount extends Configured with Tool with WritableConversions with Toke
}
}

// Same as above, except using explicit loops to look more like pseudo-code
class AlternativeMapper extends Mapper[LongWritable, Text, Text, IntWritable] {
override def map(key: LongWritable, value: Text,
context: Mapper[LongWritable, Text, Text, IntWritable]#Context) = {
for (word <- tokenize(value)) {
context.write(word, 1)
}
}
}

class MyMapperHistogram extends Mapper[LongWritable, Text, Text, IntWritable] {
override def map(key: LongWritable, value: Text,
context: Mapper[LongWritable, Text, Text, IntWritable]#Context) = {
Expand All @@ -62,6 +72,20 @@ object WordCount extends Configured with Tool with WritableConversions with Toke
}
}

// Same as above, except using explicit loops to look more like pseudo-code
class AlternativeMapperHistogram extends Mapper[LongWritable, Text, Text, IntWritable] {
override def map(key: LongWritable, value: Text,
context: Mapper[LongWritable, Text, Text, IntWritable]#Context) = {
val counts = new HashMap[String, Int]() { override def default(key: String) = 0 }
for (word <- tokenize(value)) {
counts(word) += 1
}
for ((k, v) <- counts) {
context.write(k, v)
}
}
}

class MyMapperIMC extends Mapper[LongWritable, Text, Text, IntWritable] {
val counts = new HashMap[String, Int]().withDefaultValue(0)

Expand All @@ -75,6 +99,24 @@ object WordCount extends Configured with Tool with WritableConversions with Toke
}
}

// Same as above, except using explicit loops to look more like pseudo-code
class AlternativeMapperIMC extends Mapper[LongWritable, Text, Text, IntWritable] {
val counts = new HashMap[String, Int]().withDefaultValue(0)

override def map(key: LongWritable, value: Text,
context: Mapper[LongWritable, Text, Text, IntWritable]#Context) = {
for (word <- tokenize(value)) {
counts(word) += 1
}
}

override def cleanup(context: Mapper[LongWritable, Text, Text, IntWritable]#Context) = {
for ((k, v) <- counts) {
context.write(k, v)
}
}
}

class MyReducer extends Reducer[Text, IntWritable, Text, IntWritable] {
override def reduce(key: Text, values: java.lang.Iterable[IntWritable],
context: Reducer[Text, IntWritable, Text, IntWritable]#Context) = {
Expand Down

0 comments on commit 0bdde0a

Please sign in to comment.