Kafka

Consumer Group Example

Consumer Group Example

 

The consumer is the one who consumes data from a certain partition from the Kafka cluster. It knows from which broker to read or consume data on a specific topic.

 

Consumer Groups, on the other hand, consist of a group of more than one consumer and each customer reads or consumes data from the assigned partitions. If the number of consumers will exceed the number of partitions, the new consumers will go into the inactive state until an existing customer unsubscribes.    

 

Consider this scenario with two consumers -

 

Let us assume there are two groups of consumers- Consumer Group A and Consumer Group B. If the consumers from Consumer Group A are reading data from different partitions, it implies that they are reading it parallel under Topic - T and therefore will remain in an active state.

Now, if one of the consumers of Group B reads data from Partition - 1 under Topic - T, then also the consumer remains in an active state as it belongs to Group B. 

 

Consider another scenario with three consumers -

 

There are three consumers with two partitions available. Two consumers - Consumer A and Consumer B are in an active state with consumer A reading data from Partition 0 and consumer B reading from Partition 1. Consumer C will remain in an inactive state unless consumer A or consumer B leaves. 

Now let us see examples of how to send and receive messages through Java client.

 

Here’s how you can create a sample group with two consumers -

 

>>java -cp “/path/to/kafka/kafka_2.11-0.9.0.0/libs/*":. 

ConsumerGroup <topic-name> my-group

>>java -cp "/home/bala/Workspace/kafka/kafka_2.11-0.9.0.0/libs/*":. 

ConsumerGroup <topic-name> my-group

 

Now send messages using Producer CLI -

 

Test consumer group 01

Test consumer group 02

 

It will give the output of the first and the second process respectively as -

 

Subscribed to topic Hello-kafka

offset = 3, key = null, value = Test consumer group 01

 

And 

 

Subscribed to topic Hello-kafka

offset = 3, key = null, value = Test consumer group 02

 

Top course recommendations for you

    Blockchain Process
    1 hrs
    Beginner
    7K+ Learners
    4.59  (663)
    GO Programming Language
    1 hrs
    Beginner
    6.8K+ Learners
    4.49  (532)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    17.6K+ Learners
    4.52  (1682)
    Fibonacci Series in Python
    1 hrs
    Beginner
    2.2K+ Learners
    4.61  (92)
    Design App
    1 hrs
    Beginner
    18.3K+ Learners
    4.45  (1208)
    Pivot Tables in Excel
    1 hrs
    Beginner
    22.1K+ Learners
    4.57  (2734)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    2.3K+ Learners
    4.59  (376)
    QR Code Generator in Python
    1 hrs
    Beginner
    4.8K+ Learners
    4.36  (212)
    Backtracking Algorithm
    1 hrs
    Beginner
    2.7K+ Learners
    4.55  (286)
    Bitcoin for Beginners
    1 hrs
    Beginner
    6.9K+ Learners
    4.53  (432)