Skip to content

Instantly share code, notes, and snippets.

@rajkrrsingh
Last active September 11, 2017 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajkrrsingh/6a8bd45c1dbfb2a91376a163f2431eb1 to your computer and use it in GitHub Desktop.
Save rajkrrsingh/6a8bd45c1dbfb2a91376a163f2431eb1 to your computer and use it in GitHub Desktop.
sample kafka producer using python
yum install -y python-pip
pip install kafka-python
//kafka producer sample code
vim kafka_producer.py
from kafka import KafkaProducer
from kafka.errors import KafkaError
producer = KafkaProducer(bootstrap_servers=['rkk1.hdp.local:6667'])
topic = "kafkatopic"
producer.send(topic, b'test message')
//run it
python kafka_producer.py
//test it
[root@rkk1 ~]# /usr/hdp/current/kafka-broker/bin/kafka-console-consumer.sh --zookeeper `hostname`:2181 --topic kafkatopic
{metadata.broker.list=rkk1.hdp.local:6667,rkk2.hdp.local:6667,rkk3.hdp.local:6667, request.timeout.ms=30000, client.id=console-consumer-41051, security.protocol=PLAINTEXT}
test message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment