17
17
18
18
# Example use of AdminClient operations.
19
19
20
+ from confluent_kafka import (KafkaException , ConsumerGroupTopicPartitions ,
21
+ TopicPartition , ConsumerGroupState )
20
22
from confluent_kafka .admin import (AdminClient , NewTopic , NewPartitions , ConfigResource , ConfigSource ,
21
- AclBinding , AclBindingFilter , ResourceType , ResourcePatternType ,
22
- AclOperation , AclPermissionType )
23
- from confluent_kafka import KafkaException
23
+ AclBinding , AclBindingFilter , ResourceType , ResourcePatternType , AclOperation ,
24
+ AclPermissionType )
24
25
import sys
25
26
import threading
26
27
import logging
@@ -419,17 +420,146 @@ def example_list(a, args):
419
420
print (" {} consumer groups" .format (len (groups )))
420
421
for g in groups :
421
422
if g .error is not None :
422
- errstr = ": {}" .format (t .error )
423
+ errstr = ": {}" .format (g .error )
423
424
else :
424
425
errstr = ""
425
426
426
427
print (" \" {}\" with {} member(s), protocol: {}, protocol_type: {}{}" .format (
427
- g , len (g .members ), g .protocol , g .protocol_type , errstr ))
428
+ g , len (g .members ), g .protocol , g .protocol_type , errstr ))
428
429
429
430
for m in g .members :
430
431
print ("id {} client_id: {} client_host: {}" .format (m .id , m .client_id , m .client_host ))
431
432
432
433
434
+ def example_list_consumer_groups (a , args ):
435
+ """
436
+ List Consumer Groups
437
+ """
438
+ states = {ConsumerGroupState [state ] for state in args }
439
+ future = a .list_consumer_groups (request_timeout = 10 , states = states )
440
+ try :
441
+ list_consumer_groups_result = future .result ()
442
+ print ("{} consumer groups" .format (len (list_consumer_groups_result .valid )))
443
+ for valid in list_consumer_groups_result .valid :
444
+ print (" id: {} is_simple: {} state: {}" .format (
445
+ valid .group_id , valid .is_simple_consumer_group , valid .state ))
446
+ print ("{} errors" .format (len (list_consumer_groups_result .errors )))
447
+ for error in list_consumer_groups_result .errors :
448
+ print (" error: {}" .format (error ))
449
+ except Exception :
450
+ raise
451
+
452
+
453
+ def example_describe_consumer_groups (a , args ):
454
+ """
455
+ Describe Consumer Groups
456
+ """
457
+
458
+ futureMap = a .describe_consumer_groups (args , request_timeout = 10 )
459
+
460
+ for group_id , future in futureMap .items ():
461
+ try :
462
+ g = future .result ()
463
+ print ("Group Id: {}" .format (g .group_id ))
464
+ print (" Is Simple : {}" .format (g .is_simple_consumer_group ))
465
+ print (" State : {}" .format (g .state ))
466
+ print (" Partition Assignor : {}" .format (g .partition_assignor ))
467
+ print (" Coordinator : ({}) {}:{}" .format (g .coordinator .id , g .coordinator .host , g .coordinator .port ))
468
+ print (" Members: " )
469
+ for member in g .members :
470
+ print (" Id : {}" .format (member .member_id ))
471
+ print (" Host : {}" .format (member .host ))
472
+ print (" Client Id : {}" .format (member .client_id ))
473
+ print (" Group Instance Id : {}" .format (member .group_instance_id ))
474
+ if member .assignment :
475
+ print (" Assignments :" )
476
+ for toppar in member .assignment .topic_partitions :
477
+ print (" {} [{}]" .format (toppar .topic , toppar .partition ))
478
+ except KafkaException as e :
479
+ print ("Error while describing group id '{}': {}" .format (group_id , e ))
480
+ except Exception :
481
+ raise
482
+
483
+
484
+ def example_delete_consumer_groups (a , args ):
485
+ """
486
+ Delete Consumer Groups
487
+ """
488
+ groups = a .delete_consumer_groups (args , request_timeout = 10 )
489
+ for group_id , future in groups .items ():
490
+ try :
491
+ future .result () # The result itself is None
492
+ print ("Deleted group with id '" + group_id + "' successfully" )
493
+ except KafkaException as e :
494
+ print ("Error deleting group id '{}': {}" .format (group_id , e ))
495
+ except Exception :
496
+ raise
497
+
498
+
499
+ def example_list_consumer_group_offsets (a , args ):
500
+ """
501
+ List consumer group offsets
502
+ """
503
+
504
+ topic_partitions = []
505
+ for topic , partition in zip (args [1 ::2 ], args [2 ::2 ]):
506
+ topic_partitions .append (TopicPartition (topic , int (partition )))
507
+ if len (topic_partitions ) == 0 :
508
+ topic_partitions = None
509
+ groups = [ConsumerGroupTopicPartitions (args [0 ], topic_partitions )]
510
+
511
+ futureMap = a .list_consumer_group_offsets (groups )
512
+
513
+ for group_id , future in futureMap .items ():
514
+ try :
515
+ response_offset_info = future .result ()
516
+ print ("Group: " + response_offset_info .group_id )
517
+ for topic_partition in response_offset_info .topic_partitions :
518
+ if topic_partition .error :
519
+ print (" Error: " + topic_partition .error .str () + " occurred with " +
520
+ topic_partition .topic + " [" + str (topic_partition .partition ) + "]" )
521
+ else :
522
+ print (" " + topic_partition .topic +
523
+ " [" + str (topic_partition .partition ) + "]: " + str (topic_partition .offset ))
524
+
525
+ except KafkaException as e :
526
+ print ("Failed to list {}: {}" .format (group_id , e ))
527
+ except Exception :
528
+ raise
529
+
530
+
531
+ def example_alter_consumer_group_offsets (a , args ):
532
+ """
533
+ Alter consumer group offsets
534
+ """
535
+
536
+ topic_partitions = []
537
+ for topic , partition , offset in zip (args [1 ::3 ], args [2 ::3 ], args [3 ::3 ]):
538
+ topic_partitions .append (TopicPartition (topic , int (partition ), int (offset )))
539
+ if len (topic_partitions ) == 0 :
540
+ topic_partitions = None
541
+ groups = [ConsumerGroupTopicPartitions (args [0 ], topic_partitions )]
542
+
543
+ futureMap = a .alter_consumer_group_offsets (groups )
544
+
545
+ for group_id , future in futureMap .items ():
546
+ try :
547
+ response_offset_info = future .result ()
548
+ print ("Group: " + response_offset_info .group_id )
549
+ for topic_partition in response_offset_info .topic_partitions :
550
+ if topic_partition .error :
551
+ print (" Error: " + topic_partition .error .str () + " occurred with " +
552
+ topic_partition .topic + " [" + str (topic_partition .partition ) + "]" )
553
+ else :
554
+ print (" " + topic_partition .topic +
555
+ " [" + str (topic_partition .partition ) + "]: " + str (topic_partition .offset ))
556
+
557
+ except KafkaException as e :
558
+ print ("Failed to alter {}: {}" .format (group_id , e ))
559
+ except Exception :
560
+ raise
561
+
562
+
433
563
if __name__ == '__main__' :
434
564
if len (sys .argv ) < 3 :
435
565
sys .stderr .write ('Usage: %s <bootstrap-brokers> <operation> <args..>\n \n ' % sys .argv [0 ])
@@ -449,6 +579,14 @@ def example_list(a, args):
449
579
sys .stderr .write (' delete_acls <resource_type1> <resource_name1> <resource_patter_type1> ' +
450
580
'<principal1> <host1> <operation1> <permission_type1> ..\n ' )
451
581
sys .stderr .write (' list [<all|topics|brokers|groups>]\n ' )
582
+ sys .stderr .write (' list_consumer_groups [<state1> <state2> ..]\n ' )
583
+ sys .stderr .write (' describe_consumer_groups <group1> <group2> ..\n ' )
584
+ sys .stderr .write (' delete_consumer_groups <group1> <group2> ..\n ' )
585
+ sys .stderr .write (' list_consumer_group_offsets <group> [<topic1> <partition1> <topic2> <partition2> ..]\n ' )
586
+ sys .stderr .write (
587
+ ' alter_consumer_group_offsets <group> <topic1> <partition1> <offset1> ' +
588
+ '<topic2> <partition2> <offset2> ..\n ' )
589
+
452
590
sys .exit (1 )
453
591
454
592
broker = sys .argv [1 ]
@@ -467,7 +605,12 @@ def example_list(a, args):
467
605
'create_acls' : example_create_acls ,
468
606
'describe_acls' : example_describe_acls ,
469
607
'delete_acls' : example_delete_acls ,
470
- 'list' : example_list }
608
+ 'list' : example_list ,
609
+ 'list_consumer_groups' : example_list_consumer_groups ,
610
+ 'describe_consumer_groups' : example_describe_consumer_groups ,
611
+ 'delete_consumer_groups' : example_delete_consumer_groups ,
612
+ 'list_consumer_group_offsets' : example_list_consumer_group_offsets ,
613
+ 'alter_consumer_group_offsets' : example_alter_consumer_group_offsets }
471
614
472
615
if operation not in opsmap :
473
616
sys .stderr .write ('Unknown operation: %s\n ' % operation )
0 commit comments