简体   繁体   中英

kafka API for Reading kafka messages in a Group from topic with same partition key

I have 5 million of messages in internal Kafka Topic.

1 million  message with Partition key  -1234-Messge1
1 million  message with Partition key  -2345-Messge2
1 million  message with Partition key  -5678-Messge3
1 million  message with Partition key  -6789-Messge4
1 million  message with Partition key  -6565-Messge5

I have to join messages having same Partitionkey as part of single message and send to an consumer Topic [Eg: For key 1234-Messge1 ,consumer should receive single message instead of 1 million message]

Is there any Kafka API available at kafka end ,using which I can read all the messages having same Partition key in a group instead of reading single message at a time like traditional spring boot kafka Listener.

In Kafka Streams, you can filter on a specific record key, but this will read all the partitions. You can also groupByKey if you wanted the same logic for all keys, and can query all values from a KTable , for example.

If you already know (or can compute) the topic partition, you can assign (or use @KafkaListener property topicPartitions ) to make a Consumer read one-to-many TopicPartition . This will still read all records for that partition, and if you have multiple keys, you would need if (record.key().equals(partitionKey))

Not really, as Kafka does not provide key-driven API. In the end (whether you use KafkaConsumer directly or Streams), you are going to read all records from the partitions you want.

If you know what partition(s) your records are in, then you might set up the consumers to read from these partition(s).

However please remember you might have been impacted by situation like increasing number of partition in a topic (what changes a hash function, unless you use stable hashing partitioner).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM