Skip to content

Kafka

Getting Started

Apache Kafka — Overview

Example Connection Code

$ pip install kafka-python
from kafka import KafkaProducer
import json

producer = KafkaProducer(
    bootstrap_servers='localhost:29092',
    security_protocol="PLAINTEXT",
    value_serializer=lambda v: json.dumps(v).encode('ascii')
)

producer.send(
 'hotel-booking-request',
 value={
     "name": "Evy Lina",
     "hotel": "Cheap Hotel",
     "dateFrom": "14-07-2024",
     "dateTo": "01-08-2021",
     "details": "Wish coffee ready 😀"
     }
)
producer.flush()
from kafka import KafkaConsumer
import json

consumer = KafkaConsumer(
    bootstrap_servers='localhost:29092',
    security_protocol="PLAINTEXT",
    value_deserializer=lambda v: json.loads(v.decode('ascii')),
    # auto_offset_reset='earliest'
)

consumer.subscribe(topics='hotel-booking-request')

for message in consumer:
    print(f"{message.partition}:{message.offset} v={message.value}")