forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_basic.py
More file actions
80 lines (63 loc) · 2.38 KB
/
Copy pathexample_basic.py
File metadata and controls
80 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import time
from datetime import datetime
from threading import Thread
from kafka import KafkaConsumer, KafkaProducer
from testcontainers.kafka import KafkaContainer
def basic_example():
with KafkaContainer() as kafka:
# Get connection parameters
bootstrap_servers = kafka.get_bootstrap_server()
# Create Kafka producer
producer = KafkaProducer(
bootstrap_servers=bootstrap_servers, value_serializer=lambda v: json.dumps(v).encode("utf-8")
)
print("Created Kafka producer")
# Create Kafka consumer
consumer = KafkaConsumer(
bootstrap_servers=bootstrap_servers,
value_deserializer=lambda v: json.loads(v.decode("utf-8")),
auto_offset_reset="earliest",
group_id="test_group",
)
print("Created Kafka consumer")
# Define topics
topics = ["test_topic1", "test_topic2"]
# Subscribe to topics
consumer.subscribe(topics)
print(f"Subscribed to topics: {topics}")
# Start consuming in a separate thread
def consume_messages():
for message in consumer:
print(f"\nReceived message from {message.topic}:")
print(json.dumps(message.value, indent=2))
consumer_thread = Thread(target=consume_messages)
consumer_thread.daemon = True
consumer_thread.start()
# Produce test messages
test_messages = [
{
"topic": "test_topic1",
"message": {"id": 1, "content": "Message for topic 1", "timestamp": datetime.utcnow().isoformat()},
},
{
"topic": "test_topic2",
"message": {"id": 2, "content": "Message for topic 2", "timestamp": datetime.utcnow().isoformat()},
},
]
for msg in test_messages:
producer.send(msg["topic"], msg["message"])
print(f"Sent message to {msg['topic']}")
# Wait for messages to be processed
time.sleep(2)
# Get topic information
print("\nTopic information:")
for topic in topics:
partitions = consumer.partitions_for_topic(topic)
print(f"{topic}:")
print(f" Partitions: {partitions}")
# Clean up
producer.close()
consumer.close()
if __name__ == "__main__":
basic_example()