aboutsummaryrefslogtreecommitdiff
path: root/wordcount/multilang
diff options
context:
space:
mode:
Diffstat (limited to 'wordcount/multilang')
-rw-r--r--wordcount/multilang/resources/countbolt.py26
-rw-r--r--wordcount/multilang/resources/sentencespout.py28
-rw-r--r--wordcount/multilang/resources/splitbolt.py21
3 files changed, 75 insertions, 0 deletions
diff --git a/wordcount/multilang/resources/countbolt.py b/wordcount/multilang/resources/countbolt.py
new file mode 100644
index 0000000..4aebc19
--- /dev/null
+++ b/wordcount/multilang/resources/countbolt.py
@@ -0,0 +1,26 @@
+import storm
+# Counter is a nice way to count things,
+# but it is a Python 2.7 thing
+from collections import Counter
+
+class CountBolt(storm.BasicBolt):
+ # Initialize this instance
+ def initialize(self, conf, context):
+ self._conf = conf
+ self._context = context
+ # Create a new counter for this instance
+ self._counter = Counter()
+ storm.logInfo("Counter bolt instance starting...")
+
+ def process(self, tup):
+ # Get the word from the inbound tuple
+ word = tup.values[0]
+ # Increment the counter
+ self._counter[word] +=1
+ count = self._counter[word]
+ storm.logInfo("Emitting %s:%s" % (word, count))
+ # Emit the word and count
+ storm.emit([word, count])
+
+# Start the bolt when it's invoked
+CountBolt().run()
diff --git a/wordcount/multilang/resources/sentencespout.py b/wordcount/multilang/resources/sentencespout.py
new file mode 100644
index 0000000..a75f37d
--- /dev/null
+++ b/wordcount/multilang/resources/sentencespout.py
@@ -0,0 +1,28 @@
+import storm
+import random
+# Define some sentences
+SENTENCES = """
+the cow jumped over the moon
+an apple a day keeps the doctor away
+four score and seven years ago
+snow white and the seven dwarfs
+i am at two with nature
+""".strip().split('\n')
+
+class SentenceSpout(storm.Spout):
+ # Not much to do here for such a basic spout
+ def initialize(self, conf, context):
+ self._conf = conf
+ self._context = context
+
+ storm.logInfo("Spout instance starting...")
+
+ # Process the next tuple
+ def nextTuple(self):
+ # Emit a random sentence
+ sentence = random.choice(SENTENCES)
+ storm.logInfo("Emiting %s" % sentence)
+ storm.emit([sentence])
+
+# Start the spout when it's invoked
+SentenceSpout().run()
diff --git a/wordcount/multilang/resources/splitbolt.py b/wordcount/multilang/resources/splitbolt.py
new file mode 100644
index 0000000..b46a901
--- /dev/null
+++ b/wordcount/multilang/resources/splitbolt.py
@@ -0,0 +1,21 @@
+import storm
+
+class SplitBolt(storm.BasicBolt):
+ # There's nothing to initialize here,
+ # since this is just a split and emit
+ # Initialize this instance
+ def initialize(self, conf, context):
+ self._conf = conf
+ self._context = context
+ storm.logInfo("Split bolt instance starting...")
+
+ def process(self, tup):
+ # Split the inbound sentence at spaces
+ words = tup.values[0].split()
+ # Loop over words and emit
+ for word in words:
+ storm.logInfo("Emitting %s" % word)
+ storm.emit([word])
+
+# Start the bolt when it's invoked
+SplitBolt().run()