In the previous example, we agreed that the consumer was a manual commit, and we have read messages
from the same topic as before it was numbers, but we have agreed that the bar for that contains actually
a list of consumer records.
And while looping through records that we received from brokers, we have added records to the bathroom.
And as soon as the bar size reaches this minimum limit that is set here by each size, then we write
the contents of the bathroom to file.
Actually, you are able to perform any other operations you want.
And afterwards we sent manual commit the message to the broker.
We confirm that we have received a set of messages and afterwards weekly our bathroom.
That's what we have done in the previous example.
Now let's go back to our first example, this one consumer with all the commit.
And based on this example, let's create the consumer that will connect only two particular partitions
instead of entire topic.
And for that, we will use here a sign method instead of subscribe method.
OK, let me quote the contents of this entire file.
And I don't need the consumer was manual commit and they don't need to do this as well.
Let's create new glass.
Right, Miles, click continue.
And the Java class and name for this class will be consumer with the partitions assignment like this.
And now let me select this deleted and paste of what I have copied from consumer without a comment,
and let's rename this glass to Consumer with Partitions assignment like so no, let me hide those windows,
this one and this one.
And no, let's modify this consumer class.
And instead of subscribing here, we will use consumer assign method.
And if you'll go to the recommendation, you will be able to find a similar example here.
In this example, there are two partitions and we create a new topic, partitions like so, and afterwards
we use them in a method like this.
OK, let's go here.
And first of all, let's remove these topics variable and simply keep numbers variable as doing it will
be topic.
And here we will not use consumer to subscribe.
Instead, we will use consumer to assign method.
And here we will pass the array of partitions like this, and here we will use the topic protection
class.
If you'll press step here, it will be imported from Aperture Kafka dot com on and here will be an array
of partitions like this.
And let's first assign this consumer only to a single partition with a deep will say two.
For this you need to use here assignment operator and here will be Array and the Hills create a new
instance of topic partition and first argument here will be strewing and the second argument will be
partition.
Here we will use the topic variable as a topic name and the partition will be simpler to like solve
and notice that the label will be added automatically.
And now we are actually good to go and we are able to start this consumer and see whether it will read
messages or not.
Notice that the remaining part in this example, I'll leave unchanged and the we will be able to see
partition.
No, actually here in time.
OK, let's open up terminal and froster, please close.
And also consumers that are running will stop this one.
Glos this and now let's around the consumer with partitions assignment.
But before that, let me adjust the group idea and let's make it equal to the old group like that.
And now let's go to run and run consumer with partitions.
Assignment, main method.
It seems that the consumer is now up and running and now let's go to our producer and generate some
messages and we should expect that we will see the messages from Partition with Editta.
So let's start our producer around producer, that main it has produced 100 messages.
Let's go to consumer type.
And if you scroll up, you'll see that it has received messages only from competition with Ideato,
and that's what we actually expected.
Let's also modify this array and let's assign one more partition to do the consumer and you could use
the syntax like.
So let me split it to different lines like this.
And here on the next line will be new topic partition.
And here will be a topic.
And let's assign a partition with a let's say for like this.
And let's now rerun the consumer with partitions assignment and produce some messages more.
So let's restarted started.
Consumer was that let's go to producer, run it over again, go back to consumer, and now we should
expect to see messages from two partitions with Editta and with a default.
And yes, it is the case here.
This is how you are able to assign consumer to particular partitions.
For that, you should use a sign method instead of subscribe.
But please know that you are not able to combine, assign and subscribe method.
You are not able to use them simultaneously.
You will get an error and such case.
This also this example.
And I will see you next guys by.