Skip to content

Commit 8576b84

Browse files
committed
Fix: Wrapping message_body with dict
1 parent 006e2b1 commit 8576b84

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

examples/mysql_to_kafka.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def create_kafka_topic(topic_name, num_partitions=1, replication_factor=1):
2323
topic = NewTopic(
2424
name=topic_name,
2525
num_partitions=num_partitions,
26-
replication_factor=replication_factor
26+
replication_factor=replication_factor,
2727
)
2828

2929
admin_client.create_topics(new_topics=[topic])
@@ -32,18 +32,17 @@ def create_kafka_topic(topic_name, num_partitions=1, replication_factor=1):
3232
def main():
3333
global message_body
3434
producer = KafkaProducer(
35-
bootstrap_servers='127.0.0.1:9092',
36-
value_serializer=lambda v: str(v).encode('utf-8')
35+
bootstrap_servers="127.0.0.1:9092",
36+
value_serializer=lambda v: str(v).encode("utf-8"),
3737
)
3838

3939
stream = BinLogStreamReader(
4040
connection_settings=MYSQL_SETTINGS,
4141
server_id=3,
42-
only_events=[DeleteRowsEvent,UpdateRowsEvent,WriteRowsEvent]
42+
only_events=[DeleteRowsEvent, UpdateRowsEvent, WriteRowsEvent],
4343
)
4444

4545
for binlogevent in stream:
46-
4746
for row in binlogevent.rows:
4847
if isinstance(binlogevent, DeleteRowsEvent):
4948
topic = "deleted"
@@ -57,20 +56,20 @@ def main():
5756
topic = "created"
5857
message_body = row["values"].items()
5958

60-
producer.send(topic, key=None, value=message_body)
59+
producer.send(topic, key=None, value=dict(message_body))
6160

6261
consumer = KafkaConsumer(
63-
'deleted',
64-
'updated',
65-
'created',
66-
bootstrap_servers='127.0.0.1:9092',
67-
value_deserializer=lambda x: x.decode('utf-8'),
68-
auto_offset_reset='earliest',
69-
group_id = '1'
62+
"deleted",
63+
"updated",
64+
"created",
65+
bootstrap_servers="127.0.0.1:9092",
66+
value_deserializer=lambda x: x.decode("utf-8"),
67+
auto_offset_reset="earliest",
68+
group_id="1",
7069
)
7170

7271
for message in consumer:
73-
print(f"Topic: {message.topic}, Value: {message.value}")
72+
print(f'Topic: "{message.topic}", Value: "{message.value}"')
7473

7574
stream.close()
7675
producer.close()

0 commit comments

Comments
 (0)