Advertisement
nuclearfossil

Simple Publisher for the hello-samza modificaiton

Mar 23rd, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. from kafka.client import KafkaClient
  2. from kafka.consumer import SimpleConsumer
  3. from kafka.producer import SimpleProducer
  4.  
  5. import time
  6. import random
  7. import logging
  8.  
  9. logging.basicConfig(level=logging.INFO)
  10.  
  11. s_nouns = ["A dude", "My mom", "The king", "Some guy", "A cat with rabies", "A sloth", "Your homie", "This cool guy my gardener met yesterday", "Superman"]
  12. p_nouns = ["These dudes", "Both of my moms", "All the kings of the world", "Some guys", "All of a cattery's cats", "The multitude of sloths living under your bed", "Your homies", "Like, these, like, all these people", "Supermen"]
  13. s_verbs = ["eats", "kicks", "gives", "treats", "meets with", "creates", "hacks", "configures", "spies on", "retards", "meows on", "flees from", "tries to automate", "explodes"]
  14. p_verbs = ["eat", "kick", "give", "treat", "meet with", "create", "hack", "configure", "spy on", "retard", "meow on", "flee from", "try to automate", "explode"]
  15. infinitives = ["to make a pie.", "for no apparent reason.", "because the sky is green.", "for a disease.", "to be able to make toast explode.", "to know more about archeology."]
  16.  
  17. broker = 'localhost:9092'
  18. logging.info("Connecting to Kafka - {0}".format(broker))
  19. client = KafkaClient(broker)
  20. producer = SimpleProducer(client)
  21.  
  22. # write out 1000 random sentences
  23. index = 0
  24. logging.info("sending strings")
  25. while index < 10000:
  26.     index += 1
  27.     sentence = random.choice(s_nouns) + " " + \
  28.         random.choice(s_verbs) + " " + \
  29.         random.choice(s_nouns).lower() or random.choice(p_nouns).lower() + " " + \
  30.         random.choice(infinitives)
  31.     producer.send_messages('myTopic', sentence)
  32.     logging.info("sent {0} of 10000".format(index))
  33.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement