Apache Kafka
Apache Kafka
SOLUTION:
Prerequisites:
Create a simple Python script to act as a data producer. This script will simulate pulling information
from a data source and publishing it to the Kafka topic.
import json
import mysql.connector
def fetch_data_from_mysql_with_column_names():
connection = mysql.connector.connect(
host='localhost',
user='gow',
password='Gow2210***',
database='employee'
cursor = connection.cursor()
rows = cursor.fetchall()
connection.close()
producer = KafkaProducer(bootstrap_servers='localhost:9092',
value_serializer=lambda x: json.dumps(x).encode('utf-8'))
producer.send(topic, value=row)
producer.flush()
producer.close()
serializable_rows = []
serializable_row = {}
if isinstance(value, (date, datetime)): # Convert date objects to ISO 8601 formatted strings
serializable_row[column_name] = value.isoformat()
serializable_row[column_name] = float(value)
serializable_row[column_name] = bool(value)
else:
serializable_row[column_name] = value
serializable_rows.append(serializable_row)
return serializable_rows
if __name__ == "__main__":
topic = 'report_doc'
produce_to_kafka(serializable_data, topic)
Step 3: Data Consumer (Reporting Service)
Create another Python script to act as a data consumer, which will pull information from the Kafka
topic and process it for reporting purposes.
import json
import smtplib
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = receiver_email
text = body
html = f"""\
<html>
<body>
<p>{body}</p>
</body>
</html>
"""
msg.attach(part2)
server.starttls()
server.login(sender_email, sender_password)
consumer = KafkaConsumer(topic,
bootstrap_servers='localhost:9092',
value_deserializer=lambda x: json.loads(x.decode('utf-8')))
data = message.value
if __name__ == "__main__":
topic = 'report_doc'
sender_email = '[email protected]'
receiver_email = '[email protected]'