Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hrhouma authored May 9, 2024
1 parent 0b7393b commit b28cb21
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions A19 - Kafka/1-présentationKAFKA/consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Importing necessary libraries
from kafka import KafkaConsumer
import json

# Configuration for Kafka consumer
consumer_conf = {
# Set the bootstrap servers based on your Kafka cluster and host names
# For a single node (localhost) setup, use 'localhost:9092'
'bootstrap_servers': ['localhost:9092'],
'group_id': 'my_group',
'auto_offset_reset': 'latest',
'value_deserializer': lambda v: json.loads(v.decode('utf-8'))
}

# Creating Kafka consumer instance
consumer = KafkaConsumer('my-topic-test', **consumer_conf)

# Consuming messages from Kafka
for message in consumer:
print(f"Received message: {message.value}")

# Closing the consumer
consumer.close()
30 changes: 30 additions & 0 deletions A19 - Kafka/1-présentationKAFKA/producer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Importing necessary libraries
import json
from kafka import KafkaProducer
import time

# Configuration for Kafka producer
producer_conf = {
# Set the bootstrap servers based on your Kafka cluster and host names
# For example, 'your-host-name:9092'
'bootstrap_servers': ['localhost:9092'],
'value_serializer': lambda v: json.dumps(v).encode('utf-8')
}

# Creating Kafka producer instance
producer = KafkaProducer(**producer_conf)

# Define the topic name
topic_name = 'my-topic-test'

# Sending messages to Kafka
for i in range(100):
message = {'this is message #': i}

producer.send(topic_name, value=message)
print(f'Message sent: {message}')
time.sleep(1)

# Flush and close the producer
producer.flush()
producer.close()

0 comments on commit b28cb21

Please sign in to comment.