Uh oh!
There was an error while loading. Please reload this page.
forked from dpkp/kafka-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_example.py
More file actions
Latest commit
94 lines (71 loc) · 2.43 KB
/
Copy pathaws_example.py
File metadata and controls
94 lines (71 loc) · 2.43 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
"""
written by jeff
"""
importthreading, time
importbotocore
fromkafkaimportKafkaAdminClient, KafkaConsumer, KafkaProducer
fromkafka.adminimportNewTopic
importsys
fromosimportenviron
BOOTSTRAP_SERVERS=environ.get("KAFKA_BROKERS")
AWS_ACCESS_KEY_ID=environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY=environ.get("AWS_SECRET_ACCESS_KEY")
AWS_REGION=environ.get("AWS_REGION")
TOPIC_NAME=environ["INTAKES_REQUEST_TOPIC"]
importlogging
logging.basicConfig(level=logging.DEBUG)
classProducer(threading.Thread):
def__init__(self):
threading.Thread.__init__(self)
self.stop_event=threading.Event()
defstop(self):
self.stop_event.set()
defrun(self):
producer=KafkaProducer(bootstrap_servers=BOOTSTRAP_SERVERS,
security_protocol='SASL_SSL',
sasl_mechanism='AWS_MSK_IAM',
api_version=(2,8,0),
)
whilenotself.stop_event.is_set():
producer.send(TOPIC_NAME, b"test")
producer.send(TOPIC_NAME, b"\xc2Hola, mundo!")
time.sleep(1)
producer.close()
classConsumer(threading.Thread):
def__init__(self):
threading.Thread.__init__(self)
self.stop_event=threading.Event()
defstop(self):
self.stop_event.set()
defrun(self):
consumer=KafkaConsumer(bootstrap_servers=BOOTSTRAP_SERVERS,
auto_offset_reset='earliest',
consumer_timeout_ms=1000,
security_protocol='SASL_SSL',
sasl_mechanism='AWS_MSK_IAM',
api_version=(2,8,0),
)
consumer.subscribe([TOPIC_NAME])
whilenotself.stop_event.is_set():
formessageinconsumer:
print(f"consumer: {message}")
ifself.stop_event.is_set():
break
consumer.close()
defmain():
tasks= [
Producer(),
# Consumer()
]
# Start threads of a publisher/producer and a subscriber/consumer to 'my-topic' Kafka topic
fortintasks:
t.start()
time.sleep(10)
# Stop threads
fortaskintasks:
task.stop()
fortaskintasks:
task.join()
if__name__=="__main__":
main()