-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming_data_reader.py
44 lines (35 loc) · 1.33 KB
/
streaming_data_reader.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
"""
Streaming data consumer
"""
from datetime import datetime
from kafka import KafkaConsumer
import mysql.connector
TOPIC='set your topic here'
DATABASE = 'set your database name here'
USERNAME = 'set your username here'
PASSWORD = 'set your database password here'
print("Connecting to the database")
try:
connection = mysql.connector.connect(host='localhost', database=DATABASE, user=USERNAME, password=PASSWORD)
except Exception:
print("Could not connect to database. Please check credentials")
else:
print("Connected to database")
cursor = connection.cursor()
print("Connecting to Kafka")
consumer = KafkaConsumer(TOPIC)
print("Connected to Kafka")
print(f"Reading messages from the topic {TOPIC}")
for msg in consumer:
# Extract information from kafka
message = msg.value.decode("utf-8")
# Transform the date format to suit the database schema
(timestamp, vehcile_id, vehicle_type, plaza_id) = message.split(",")
dateobj = datetime.strptime(timestamp, '%a %b %d %H:%M:%S %Y')
timestamp = dateobj.strftime("%Y-%m-%d %H:%M:%S")
# Loading data into the database table
sql = "insert into livetolldata values(%s,%s,%s,%s)"
result = cursor.execute(sql, (timestamp, vehcile_id, vehicle_type, plaza_id))
print(f"A {vehicle_type} was inserted into the database")
connection.commit()
connection.close()