33
33
with_progress = False
34
34
35
35
# Kafka bootstrap server(s)
36
- bootstrap_servers = 'localhost'
36
+ bootstrap_servers = None
37
37
38
+ # Topic to use
39
+ topic = 'test'
38
40
39
41
# global variable to be set by stats_cb call back function
40
42
good_stats_cb_result = False
@@ -90,22 +92,22 @@ def verify_producer():
90
92
print ('producer at %s' % p )
91
93
92
94
# Produce some messages
93
- p .produce ('test' , 'Hello Python!' )
94
- p .produce ('test' , key = 'Just a key' )
95
- p .produce ('test' , partition = 1 , value = 'Strictly for partition 1' ,
95
+ p .produce (topic , 'Hello Python!' )
96
+ p .produce (topic , key = 'Just a key' )
97
+ p .produce (topic , partition = 1 , value = 'Strictly for partition 1' ,
96
98
key = 'mykey' )
97
99
98
100
# Produce more messages, now with delivery report callbacks in various forms.
99
101
mydr = MyTestDr ()
100
- p .produce ('test' , value = 'This one has a dr callback' ,
102
+ p .produce (topic , value = 'This one has a dr callback' ,
101
103
callback = mydr .delivery )
102
- p .produce ('test' , value = 'This one has a lambda' ,
104
+ p .produce (topic , value = 'This one has a lambda' ,
103
105
callback = lambda err , msg : MyTestDr ._delivery (err , msg ))
104
- p .produce ('test' , value = 'This one has neither' )
106
+ p .produce (topic , value = 'This one has neither' )
105
107
106
108
# Produce even more messages
107
109
for i in range (0 , 10 ):
108
- p .produce ('test' , value = 'Message #%d' % i , key = str (i ),
110
+ p .produce (topic , value = 'Message #%d' % i , key = str (i ),
109
111
callback = mydr .delivery )
110
112
p .poll (0 )
111
113
@@ -123,7 +125,6 @@ def verify_producer_performance(with_dr_cb=True):
123
125
124
126
p = confluent_kafka .Producer (** conf )
125
127
126
- topic = 'test'
127
128
msgcnt = 1000000
128
129
msgsize = 100
129
130
msg_pattern = 'test.py performance'
@@ -144,9 +145,9 @@ def verify_producer_performance(with_dr_cb=True):
144
145
for i in range (0 , msgcnt ):
145
146
try :
146
147
if with_dr_cb :
147
- p .produce ('test' , value = msg_payload , callback = dr .delivery )
148
+ p .produce (topic , value = msg_payload , callback = dr .delivery )
148
149
else :
149
- p .produce ('test' , value = msg_payload )
150
+ p .produce (topic , value = msg_payload )
150
151
except BufferError as e :
151
152
# Local queue is full (slow broker connection?)
152
153
msgs_backpressure += 1
@@ -224,7 +225,7 @@ def verify_consumer():
224
225
c = confluent_kafka .Consumer (** conf )
225
226
226
227
# Subscribe to a list of topics
227
- c .subscribe (["test" ])
228
+ c .subscribe ([topic ])
228
229
229
230
max_msgcnt = 100
230
231
msgcnt = 0
@@ -270,7 +271,7 @@ def verify_consumer():
270
271
271
272
# Start a new client and get the committed offsets
272
273
c = confluent_kafka .Consumer (** conf )
273
- offsets = c .committed (list (map (lambda p : confluent_kafka .TopicPartition ("test" , p ), range (0 ,3 ))))
274
+ offsets = c .committed (list (map (lambda p : confluent_kafka .TopicPartition (topic , p ), range (0 ,3 ))))
274
275
for tp in offsets :
275
276
print (tp )
276
277
@@ -304,7 +305,7 @@ def my_on_revoke (consumer, partitions):
304
305
print (' %s [%d] @ %d' % (p .topic , p .partition , p .offset ))
305
306
consumer .unassign ()
306
307
307
- c .subscribe (["test" ], on_assign = my_on_assign , on_revoke = my_on_revoke )
308
+ c .subscribe ([topic ], on_assign = my_on_assign , on_revoke = my_on_revoke )
308
309
309
310
max_msgcnt = 1000000
310
311
bytecnt = 0
@@ -364,10 +365,11 @@ def verify_stats_cb():
364
365
def stats_cb (stats_json_str ):
365
366
global good_stats_cb_result
366
367
stats_json = json .loads (stats_json_str )
367
- if 'test' in stats_json ['topics' ]:
368
- app_offset = stats_json ['topics' ]['test' ]['partitions' ]['0' ]['app_offset' ]
368
+ if topic in stats_json ['topics' ]:
369
+ app_offset = stats_json ['topics' ][topic ]['partitions' ]['0' ]['app_offset' ]
369
370
if app_offset > 0 :
370
- print ("# app_offset stats for topic test partition 0: %d" % app_offset )
371
+ print ("# app_offset stats for topic %s partition 0: %d" % \
372
+ (topic , app_offset ))
371
373
good_stats_cb_result = True
372
374
373
375
conf = {'bootstrap.servers' : bootstrap_servers ,
@@ -381,7 +383,7 @@ def stats_cb(stats_json_str):
381
383
}}
382
384
383
385
c = confluent_kafka .Consumer (** conf )
384
- c .subscribe (["test" ])
386
+ c .subscribe ([topic ])
385
387
386
388
max_msgcnt = 1000000
387
389
bytecnt = 0
@@ -439,6 +441,11 @@ def stats_cb(stats_json_str):
439
441
440
442
if len (sys .argv ) > 1 :
441
443
bootstrap_servers = sys .argv [1 ]
444
+ if len (sys .argv ) > 2 :
445
+ topic = sys .argv [2 ]
446
+ else :
447
+ print ('Usage: %s <broker> [<topic>]' % sys .argv [0 ])
448
+ sys .exit (1 )
442
449
443
450
print ('Using confluent_kafka module version %s (0x%x)' % confluent_kafka .version ())
444
451
print ('Using librdkafka version %s (0x%x)' % confluent_kafka .libversion ())
0 commit comments