1- from confluent_kafka import Consumer , Producer
1+ import logging
2+ from confluent_kafka import Consumer , Producer , TopicPartition
3+
4+ logger = logging .getLogger ("saluki" )
25
36
47def play (
@@ -8,8 +11,21 @@ def play(
811 dest_topic : str ,
912 offsets : list [int ] | None ,
1013 timestamps : list [int ] | None ,
11- chunks : int ,
1214) -> None :
15+ """
16+ Replay data from src_topic to dest_topic between the offsets OR timestamps specified.
17+ This currently assumes contiguous data in a topic (ie. no log compaction) and only uses partition 0.
18+
19+ :param src_broker: The source broker, including port.
20+ :param src_topic: The topic to replay data from.
21+ :param dest_broker: The destination broker, including port.
22+ :param dest_topic: The topic to replay data to.
23+ :param offsets: The start and finish offsets to replay data from.
24+ :param timestamps: The start and finish timestamps to replay data from.
25+ """
26+
27+ print (f"ARGS: { src_broker } , { src_topic } , { dest_broker } , { dest_topic } , { offsets } , { timestamps } " )
28+
1329 consumer = Consumer (
1430 {
1531 "bootstrap.servers" : src_broker ,
@@ -21,4 +37,27 @@ def play(
2137 "bootstrap.servers" : dest_broker ,
2238 }
2339 )
24- pass
40+ src_partition = 0
41+
42+ if timestamps is not None :
43+ start_offset , stop_offset = consumer .offsets_for_times ([
44+ TopicPartition (src_topic , src_partition , timestamps [0 ]),
45+ TopicPartition (src_topic , src_partition , timestamps [1 ]),
46+ ])
47+ else :
48+ start_offset = TopicPartition (src_topic , src_partition , offsets [0 ])
49+ stop_offset = TopicPartition (src_topic , src_partition , offsets [1 ])
50+ consumer .assign ([start_offset ])
51+
52+ num_messages = stop_offset .offset - start_offset .offset + 1
53+
54+ try :
55+ msgs = consumer .consume (num_messages )
56+ [producer .produce (dest_topic , message .value (), message .key ()) for message in msgs ]
57+ producer .flush ()
58+ except Exception :
59+ logger .exception ("Got exception while consuming:" )
60+ finally :
61+ logger .debug (f"Closing consumer { consumer } " )
62+ consumer .close ()
63+
0 commit comments