In message queuing systems like Apache Kafka, a "producer" is responsible for sending messages to a specific topic, and a "consumer" is responsible for consuming messages from that topic.

Here's a basic example in Python to produce and consume messages using the Kafka Python library (kafka-python):

Producer:

python Code

from kafka import KafkaProducer

 

producer = KafkaProducer(bootstrap_servers='localhost:9092')

 

# send a message to topic 'test'

producer.send('test', b'Hello, World!')

 

# wait for any outstanding messages to be delivered and delivery report to be received

producer.flush()

Consumer Script:

python

from kafka import KafkaConsumer

 

consumer = KafkaConsumer('test',

                         bootstrap_servers='localhost:9092',

                         auto_offset_reset='earliest')

 

for message in consumer:

    print(f"Consumed message: {message.value}")

 

Note that this is a basic example and there are many more options and configurations you can set for both producers and consumers.

أحدث أقدم