//inspiration: http://hamletdarcy.blogspot.ca/2008/01/mapreduce-for-mere-mortals.html
// general purpose function
def mapReduceFunc = { data, mapFunction, reduceFunction ->
def mappedData = data.collect(mapFunction)
reduceFunction(mappedData)
}
// the data
Map dictionary = [
abacus: "a device for making arithmetic calculations",
arc: "any unbroken part of the circumference of a circle",
beaver: "a large, amphibious rodent of the genus Castor"
]
// the mapping function impl
def startingCharacter = { pair ->
pair.value = pair.key[0].toLowerCase()
return pair
}
// the reducing function impl
def countCharacters = { dataMap ->
Map result = [:]
('a'..'z').each{ result[it] = 0 }
dataMap.each { pair -> result[pair.value]++ }
return result
}
// put it all together!
def result1 = mapReduceFunc(dictionary, startingCharacter, countCharacters)
println result1
//// TWEAK THE I/O FUNCS! ////
// the mapping function impl
startingCharacter = { pair ->
return pair.key[0].toLowerCase()
}
// the reducing function impl
countCharacters = { dataList ->
Map result = [:]
('a'..'z').each{ result[it] = 0 }
dataList.each { it -> result[it]++ }
return result
}
// put it all together!
def result2 = mapReduceFunc(dictionary, startingCharacter, countCharacters)
println result2
assert result1 == result2
Wednesday, July 11, 2012
Conceptual Map/Reduce Example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment