6-producer.py 765 B

12345678910111213141516171819202122232425262728293031
  1. # coding:utf-8
  2. from pykafka.client import KafkaClient
  3. import logging
  4. import sys
  5. import json
  6. from pykafka.partitioners import hashing_partitioner
  7. logging.basicConfig(level = logging.INFO)
  8. producer_logger = logging.getLogger('producer')
  9. if len(sys.argv) != 4:
  10. producer_logger.warning('python producer.py id url body')
  11. sys.exit(-1)
  12. logging.basicConfig(level = logging.DEBUG)
  13. client = KafkaClient('localhost:8990,localhost:8991,localhost:8992')
  14. nmq = client.topics['nmq']
  15. producer = nmq.get_producer(partitioner = hashing_partitioner, linger_ms = 1, sync = True)
  16. request = {"url" : sys.argv[2], "body": sys.argv[3]}
  17. msg = json.dumps(request)
  18. producer.produce(msg, str(sys.argv[1]))
  19. producer_logger.info("{} has been sent successfully~".format(msg))