This Python script provides functionality for both producing and consuming messages to/from Kafka topics. It utilizes the aiokafka
library for asynchronous Kafka interactions.
- Python 3.7 or higher
- Install dependencies using
pip3 install -r requirements.txt
To produce a message to a Kafka topic, use the following command:
python app.py produce --message "your_message" --topic "your_topic" --kafka "kafka_broker_address"
--message
: The message you want to produce.--topic
: The Kafka topic to which the message will be sent.--kafka
: The address (ip:port) of the Kafka broker.
To consume messages from a Kafka topic, use the following command:
python app.py consume --topic "your_topic" --kafka "kafka_broker_address"
--topic
: The Kafka topic from which to consume messages.--kafka
: The address (ip:port) of the Kafka broker.
python app.py produce --message "Hello, Kafka!" --topic "example_topic" --kafka "localhost:9092"
python app.py consume --topic "example_topic" --kafka "localhost:9092"
- Ensure that the Kafka broker is running and accessible at the specified address.
#!/bin/bash
echo "[entrypoint] - Starting python app.py"
python3 app.py consume --topic 'YOUR_TOPIC' --kafka 'HOST:PORT'
echo "[entrypoint] - Exiting"
docker build -t your-custom-image-name .
docker run --network=mynetwork your-custom-image-name
mynetwork - your docker network with running Kafka
docker-compose -f docker-compose-kafka.yml up --build
Kafka-ui will be available on - http://localhost:8080/
- add -d to run in background
- Don't forget to replace mynetwork in docker-compose file with your bridge network name