The shortest way to produce a message to Kafka

Let’s say we have a fresh Kafka topic and we just want to send something to it, just to see how it’s being consumed. There’s my small Python script for that:

from kafka import KafkaProducer
import json
from time import strftime, gmtime

producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda v: json.dumps(v).encode('utf-8'))
topic = 'ecommerce_events'

ts = strftime("%Y-%m-%d %H:%M:%S", gmtime())

message = {'some_id': 1234, 'event_date': ts, 'event_type': 'click', 'url': ''}

future = producer.send(topic, message)

result = future.get(timeout=10)



It prepares a nice JSON message, sends it to Kafka and prints the result. Pretty handy :)

Built with Hugo
Theme Stack designed by Jimmy