Image and Documentation link: bitnami kafka
docker-compose up -d
docker exec -it <KAFKA_SERVER_CONTAINER> /bin/sh
cd opt/bitnami/kafka/bin
This location holds all .sh
files for kafka cli
kafka-topics.sh --create --bootstrap-server kafka:9092 --replication-factor 1 --partitions 1 --topic <TOPIC_NAME>
kafka-topics.sh --list --bootstrap-server kafka:9092
kafka-console-producer.sh --broker-list kafka:9092 --topic usertopic
kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic usertopic --from-beginning
We are using kafka-python
as our kafka client for python.
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='127.0.0.1:9093')
producer.send('usertopic', b'Message from Python')
producer.flush()
from kafka import KafkaConsumer
consumer = KafkaConsumer(bootstrap_servers='127.0.0.1:9093')
consumer.subscribe('usertopic')
for msg in consumer:
print(msg.value.decode('UTF-8'))
from kafka import KafkaProducer
import json
producer = KafkaProducer(bootstrap_servers='127.0.0.1:9093', value_serializer=lambda v: json.dumps(v).encode('utf-8'))
producer.send('usertopic', {'email': 'user@gmail.com'})
producer.flush()
from kafka import KafkaConsumer
import json
consumer = KafkaConsumer(bootstrap_servers='127.0.0.1:9093')
consumer.subscribe('usertopic')
for msg in consumer:
resp = json.loads(msg.value.decode('UTF-8'))
print(resp['email'])