Write a new Kafka application PartitionerDemo
(using Kafka Producer API) as follows:
- Write a custom
Partitioner
- Implement Partitioner interface
- Review Cluster (which is one of the input arguments of
Partitioner.partition
method) - Decide what to do when requested for a partition ID in
partition
method
- Write a KafkaProducer
- Register the custom
Partitioner
using ProducerConfig.PARTITIONER_CLASS_CONFIG property - Use a
Callback
input object (toProducer.send
) and display the partition ID
- Register the custom
- Create a topic with 2 partitions (on a single Kafka broker)
- Hint: Use as many
kafka-console-consumer
as many partitions are in use - Use
kafka-console-consumer
with--property print.key=true
to print keys
- Hint: Use as many
- Execute the application and the following tests. Observe the behaviour.
- Define the partition key in
Producer.send
- Don't specify the partition key
- Define the partition key in
- (EXTRA) Use
kafka-topics --alter
to increase the number of partitions and observe howCluster
reflects the change./bin/kafka-topics.sh --zookeeper :2181 --alter --topic PartitionerDemo-input --partitions 3
Duration: 30 mins