Commits

Miki Tebeka  committed bab66e3

map reduce demo

  • Participants
  • Parent commits 6d7a07e

Comments (0)

Files changed (1)

File mapreduce/wordcount.clj

+(ns wordcount
+  (:use mapreduce))
+
+(def records [
+  "Roads go ever ever on,"
+  "Over rock and under tree,"
+  "By caves where never sun has shone,"
+  "By streams that never find the sea;"
+  "Over snow by winter sown,"
+  "And through the merry flowers of June,"
+  "Over grass and over stone,"
+  "And under mountains in the moon."
+  
+  "Roads go ever ever on"
+  "Under cloud and under star,"
+  "Yet feet that wandering have gone"
+  "Turn at last to home afar."
+  "Eyes that fire and sword have seen"
+  "And horror in the halls of stone"
+  "Look at last on meadows green"
+  "And trees and hills they long have known."])
+
+(defn tokenize [text]
+  (map #(.toLowerCase %) (re-seq #"[a-zA-Z]+" text)))
+
+(defn mapper [line]
+  (for [word (tokenize line)] [word 1]))
+
+(defn reducer [key values]
+  (reduce + values))
+
+(defn main []
+  (let [result (map-reduce mapper reducer records)]
+    (dorun (map #(println (format "%s: %s" % (result %))) 
+                (reverse (sort-by result (keys result)))))
+    (shutdown-agents)))
+
+(main)
+