-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.py
50 lines (42 loc) · 1.33 KB
/
producer.py
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
import time
import json
import random
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers="localhost:9092")
if producer.bootstrap_connected() == True:
print("Yhdistetty Kafkaan onnistuneesti")
def generoi_data():
return {
"potilas_1": {
"potilas_id": 1,
"laite_id": 1,
"syke": random.randint(50, 100),
"kehon_lampotila": round(random.uniform(36.0, 37.5), 2),
"happisaturaatio": round(random.uniform(90.0, 100.0), 2),
"aikaleima": time.time(),
},
"potilas_2": {
"potilas_id": 2,
"laite_id": 2,
"syke": random.randint(65, 120),
"kehon_lampotila": round(random.uniform(37.2, 38.5), 2),
"happisaturaatio": round(random.uniform(75.0, 89), 2),
"aikaleima": time.time(),
},
}
try:
while True:
data = generoi_data()
for potilas, info in data.items():
producer.send(
topic="potilastiedot-events",
value=json.dumps(info).encode("utf-8"),
)
print(f"Lähetetään {potilas}: {info}")
time.sleep(5)
except KeyboardInterrupt:
print("Keskeytetty käyttäjän toimesta")
finally:
producer.flush()
producer.close()
print("Kafka-tuottaja suljettu")