As everyone knows the kafka producer can send message to specific patition:
producer.send(new ProducerRecord<String, String>("topic", patition,"key","value"));
Now learn to receive message from kafka consumer
just simple configuration:
1、config:
配置指定消費0和1兩個partition:
partition=0,1
2、
//get partition conf
String partition = props.getProperty("partition");
//have specific patition:
if (partition != null) {
List<TopicPartition> topicPartitions = new ArrayList<>();
String[] partitions = partition.split(",");
for (int i = 0; i < partitions.length; i++) {
TopicPartition topicPartition = new TopicPartition(topic, Integer.parseInt(partitions[i]));
topicPartitions.add(topicPartition);
}
consumer.assign(topicPartitions);
//do not specific patition :
} else {
this.consumer.subscribe(Arrays.asList(topic));
}
指定partition時使用consumer.assign() 即用戶自定義partition分配策略。
未指定時使用consumer.subscribe()即消費模式爲指定topic,由kafka指定partition分配策略。