Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- encoding: utf8 -*-
  3. from kafka import SimpleProducer, KafkaClient
  4.  
  5. # To send messages synchronously
  6. kafka = KafkaClient("localhost:4443")
  7. producer = SimpleProducer(kafka)
  8.  
  9. # Note that the application is responsible for encoding messages to type str
  10. producer.send_messages("test", "some message")
  11. producer.send_messages("test", "this method", "is variadic")
  12.  
  13. # Send unicode message
  14. producer.send_messages("test", u'你怎么样?'.encode('utf-8'))
  15.  
  16.  
  17. # To wait for acknowledgements
  18. # ACK_AFTER_LOCAL_WRITE : server will wait till the data is written to
  19. # a local log before sending response
  20. # ACK_AFTER_CLUSTER_COMMIT : server will block until the message is committed
  21. # by all in sync replicas before sending a response
  22. producer = SimpleProducer(kafka, async=False,
  23. req_acks=SimpleProducer.ACK_AFTER_LOCAL_WRITE,
  24. ack_timeout=2000)
  25.  
  26. response = producer.send_messages("test", "another message")
  27.  
  28. if response:
  29. print(response[0].error)
  30. print(response[0].offset)
  31.  
  32. # To send messages in batch. You can use any of the available
  33. # producers for doing this. The following producer will collect
  34. # messages in batch and send them to Kafka after 20 messages are
  35. # collected or every 60 seconds
  36. # Notes:
  37. # * If the producer dies before the messages are sent, there will be losses
  38. # * Call producer.stop() to send the messages and cleanup
  39. producer = SimpleProducer(kafka, batch_send=True,
  40. batch_send_every_n=20,
  41. batch_send_every_t=60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement