concept custom partitioner in category kafka

appears as: custom partitioner
Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API

This is an excerpt from Manning's book Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API.

By using a deterministic approach to select a partition, records with the same key will always be sent to the same partition and in order. The default partitioner uses this approach; if you need a different strategy for selecting partitions, you can provide a custom partitioner.

2.3.6. Writing a custom partitioner

Why would you want to write a custom partitioner? Of the several possible reasons, we’ll look at one simple case here—the use of composite keys.

Suppose you have purchase data flowing into Kafka, and the keys contain two values: a customer ID and a transaction date. But you need to group values by customer ID, so taking a hash of the customer ID and the purchase date won’t work. In this case, you’ll need to write a custom partitioner that knows which part of the composite key determines which partition to use. For example, the composite key found in src/main/java/bbejeck/model/PurchaseKey.java (source code can be found on the book’s website here: https://manning.com/books/kafka-streams-in-action) is shown in the following listing.