Kafka Streams 是如何分配分区的?
How does Kafka Streams allocate partitions?
我有一个 Kafka Streams 应用程序,它从主题 1 KStream
和主题 2 KTable
接收数据。这两个主题各有 4 个分区。假设我有 4 个应用程序实例 运行,那么每个实例将从主题 1 的单个分区接收数据。作为 KTable
收到的 topic-2 怎么样?在这种情况下,所有实例是否都会从所有 4 个分区接收数据?如果两个主题的键相同,那么我猜 Kafka Streams 将确保为应用程序分配相同的分区。如果 topic-2 没有任何键,而是应用程序将从值本身推断出它,那么这意味着所有实例都需要从 topic-2 获取所有分区。 Kafka Streams 是如何处理这种情况的?
谢谢!
KTables
根据输入分区进行分片。因此,类似于 KStream
,每个实例都会分配一个主题分区,并将该主题分区具体化为 KTable
的分片。 Kafka Streams 确保不同主题的主题分区位于同一位置,即一个实例将被分配 topic-1 partition-0
和 topic-2 partition-0
(依此类推)。
如果topic-2
没有key集,数据会随机分布在topic中。对于这种情况,您可以改用 GlobalKTable
。 GlobalKTable
是每个实例所有分区的完整复制。如果您执行 KStream-GlobalKTable-join,您可以指定一个 "mapper" 从 table 中提取连接属性(即,您可以从值中提取连接属性)。
Note: a KStream-GlobalKTable join has different semantics than a KStream-KTable join. It is not time synchronized in contrast to the later, and thus, the join is non-deterministic by design with regard to GlobalKTable updates; i.e., there is no guarantee what KStream record will be the first to "see" a GlobalKTable updates and thus join with the updated GlobalKTable record.
我有一个 Kafka Streams 应用程序,它从主题 1 KStream
和主题 2 KTable
接收数据。这两个主题各有 4 个分区。假设我有 4 个应用程序实例 运行,那么每个实例将从主题 1 的单个分区接收数据。作为 KTable
收到的 topic-2 怎么样?在这种情况下,所有实例是否都会从所有 4 个分区接收数据?如果两个主题的键相同,那么我猜 Kafka Streams 将确保为应用程序分配相同的分区。如果 topic-2 没有任何键,而是应用程序将从值本身推断出它,那么这意味着所有实例都需要从 topic-2 获取所有分区。 Kafka Streams 是如何处理这种情况的?
谢谢!
KTables
根据输入分区进行分片。因此,类似于 KStream
,每个实例都会分配一个主题分区,并将该主题分区具体化为 KTable
的分片。 Kafka Streams 确保不同主题的主题分区位于同一位置,即一个实例将被分配 topic-1 partition-0
和 topic-2 partition-0
(依此类推)。
如果topic-2
没有key集,数据会随机分布在topic中。对于这种情况,您可以改用 GlobalKTable
。 GlobalKTable
是每个实例所有分区的完整复制。如果您执行 KStream-GlobalKTable-join,您可以指定一个 "mapper" 从 table 中提取连接属性(即,您可以从值中提取连接属性)。
Note: a KStream-GlobalKTable join has different semantics than a KStream-KTable join. It is not time synchronized in contrast to the later, and thus, the join is non-deterministic by design with regard to GlobalKTable updates; i.e., there is no guarantee what KStream record will be the first to "see" a GlobalKTable updates and thus join with the updated GlobalKTable record.