我正在尝试将消息发送到一个具有4个partitions.And的主题,我希望消息到达由DefaultPartitioner决定的分区(它使用键的散列)
kafkaTemplate.sendDefault(DefaultPartitioner(job.getId()),job.getId(),job);我不确定如何让kafkaTemplate使用DefaultPartitioner来获取分区号。
有人能帮我解决这个问题吗?
发布于 2017-04-13 04:24:52
让我们从JavaDocs开始吧!
/**
* The default partitioning strategy:
* <ul>
* <li>If a partition is specified in the record, use it
* <li>If no partition is specified but a key is present choose a partition based on a hash of the key
* <li>If no partition or key is present choose a partition in a round-robin fashion
*/
public class DefaultPartitioner implements Partitioner {/**
* Send the data to the default topic with the provided key and no partition.
* @param key the key.
* @param data The data.
* @return a Future for the {@link SendResult}.
*/
ListenableFuture<SendResult<K, V>> sendDefault(K key, V data);因此,如果您希望依赖于DefaultPartitioner,那么您只需要一个记录的key。因此,只需使用特定的KafkaTemplate方法:
kafkaTemplate.sendDefault(job.getId(),job);https://stackoverflow.com/questions/43378569
复制相似问题