Kafka基于键/值对的消息传递的目的是什么? [英] What's the purpose of Kafka's key/value pair-based messaging?
问题描述
ProducerRecord
的键/值对不仅是相同的类型(所有示例均显示<String,String>
),但值相同.例如:
All of the examples of Kafka | producers show the ProducerRecord
's key/value pair as not only being the same type (all examples show <String,String>
), but the same value. For example:
producer.send(new ProducerRecord<String, String>("someTopic", Integer.toString(i), Integer.toString(i)));
但是在Kafka文档中,我似乎找不到解释键/值概念(及其潜在目的/效用)的地方.在传统的消息传递中(ActiveMQ,RabbitMQ等),我总是在特定的主题/队列/交换中触发一条消息.但是Kafka是第一个似乎要求键/值对而不是常规字符串的消息经纪人.
But in the Kafka docs, I can't seem to find where the key/value concept (and its underlying purpose/utility) is explained. In traditional messaging (ActiveMQ, RabbitMQ, etc.) I've always fired a message at a particular topic/queue/exchange. But Kafka is the first broker that seems to require key/value pairs instead of just a regulare 'ole string message.
所以我问:要求生产者发送KV对的目的/用途是什么?
推荐答案
Kafka使用由分区组成的分布式 log 的抽象.将日志拆分为多个分区可以扩展系统.
Kafka uses the abstraction of a distributed log that consists of partitions. Splitting a log into partitions allows to scale-out the system.
键用于确定日志中要附加消息的分区.而该值是消息的实际有效负载.在这方面,这些例子实际上不是很好.通常,您会使用复杂的类型作为值(例如元组类型或JSON或类似的值),并且会提取一个字段作为键.
Keys are used to determine the partition within a log to which a message get's appended to. While the value is the actual payload of the message. The examples are actually not very "good" with this regard; usually you would have a complex type as value (like a tuple-type or a JSON or similar) and you would extract one field as key.
请参阅: http://kafka.apache.org/intro#intro_topics 和 http://kafka.apache.org/intro#intro_producers
通常,键和/或值也可以是null
.如果键为null
,则将选择一个随机分区.如果值为null
,则在为主题启用日志压缩而不是日志保留策略的情况下,可以具有特殊的删除"语义(
In general the key and/or value can be null
, too. If the key is null
a random partition will the selected. If the value is null
it can have special "delete" semantics in case you enable log-compaction instead of log-retention policy for a topic (http://kafka.apache.org/documentation#compaction).
这篇关于Kafka基于键/值对的消息传递的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!