kafka主题分区的数量和数据中不同键的数量
number of kafka topic partitons and number of different keys in data
我想使用 key/value 模式写入 Kafka,以便在读取数据时保持相同的数据写入顺序。
我的问题是主题中的分区数是否应该等于传入数据中不同键的数量。
我已经知道具有相同键的 key/value 模式数据将进入同一分区。
因此,如果分区数量不等于数据中不同键的数量,我们可以在同一分区中拥有具有不同键的数据吗?在这种情况下如何保持数据顺序?
来自 Kafka 文档:
Each partition is an ordered, immutable sequence of records that is
continually appended to a structured commit log. The records in the
partitions are each assigned a sequential id number called the offset
that uniquely identifies each record within the partition.
Messages sent by a producer to a particular topic partition will be
appended in the order they are sent. That is, if a record M1 is sent
by the same producer as a record M2, and M1 is sent first, then M1
will have a lower offset than M2 and appear earlier in the log.
A
consumer instance sees records in the order they are stored in the
log.
这些是关于 Kafka 的基本规则,将具有不同密钥的消息发送到同一分区不会改变这一点。您甚至可以将所有消息发送到同一个分区,但第一条消息将在后续消息之前附加到日志中,并且具有较低的偏移值。因此将保留顺序。
My question is should the number of partitions in the topic be equal to the number of different keys in the incoming data.
我不认为这通常是个好主意。这完全取决于您正在处理的数据。如果您有固定数量的键(例如女性、男性和不同的),这可能是有意义的。然而,即使那样你也需要小心,因为这可能会导致代理上的数据负载不平衡,因为它们可能不那么多样化。因此,您最终可能会将大部分数据放在一个分区中,而其他分区将留空。一般来说,分区的数量应该根据您的吞吐量要求进行调整。
Hence if number of partitions is not equal to the number of different keys in data, we can have data having different keys in the same partition? In this case how data order is kept?
是的,您最终可能会在同一个分区中拥有不同的密钥。然后保留此特定分区的顺序,但在主题中不保证 overall.So 假设您有键 A、B 和 C 以及一个包含两个分区的主题。 A 和 C 进入第一个分区,B 存储在第二个分区中。如果数据像这样流动:
A/V1、A/V2、B/V1、C/V1、B/V2
那么你的分区将被填充成这样:
- 分区 0:A/V1、A/V2、C/V1
- 分区 1:B/V1、B/V2
使用此主题时,不清楚 A 消息和 C 消息之间的顺序与 B 消息的关系。但是始终保证消息A/V1在A/V2之前消费,A/V2在C/V1之前消费,B/V1在B/V2之前消费。
如果您正在寻找一种更灵活的方式将消息定向到分区中,您还可以考虑编写 custom partitioner。
我想使用 key/value 模式写入 Kafka,以便在读取数据时保持相同的数据写入顺序。 我的问题是主题中的分区数是否应该等于传入数据中不同键的数量。 我已经知道具有相同键的 key/value 模式数据将进入同一分区。
因此,如果分区数量不等于数据中不同键的数量,我们可以在同一分区中拥有具有不同键的数据吗?在这种情况下如何保持数据顺序?
来自 Kafka 文档:
Each partition is an ordered, immutable sequence of records that is continually appended to a structured commit log. The records in the partitions are each assigned a sequential id number called the offset that uniquely identifies each record within the partition.
Messages sent by a producer to a particular topic partition will be appended in the order they are sent. That is, if a record M1 is sent by the same producer as a record M2, and M1 is sent first, then M1 will have a lower offset than M2 and appear earlier in the log.
A consumer instance sees records in the order they are stored in the log.
这些是关于 Kafka 的基本规则,将具有不同密钥的消息发送到同一分区不会改变这一点。您甚至可以将所有消息发送到同一个分区,但第一条消息将在后续消息之前附加到日志中,并且具有较低的偏移值。因此将保留顺序。
My question is should the number of partitions in the topic be equal to the number of different keys in the incoming data.
我不认为这通常是个好主意。这完全取决于您正在处理的数据。如果您有固定数量的键(例如女性、男性和不同的),这可能是有意义的。然而,即使那样你也需要小心,因为这可能会导致代理上的数据负载不平衡,因为它们可能不那么多样化。因此,您最终可能会将大部分数据放在一个分区中,而其他分区将留空。一般来说,分区的数量应该根据您的吞吐量要求进行调整。
Hence if number of partitions is not equal to the number of different keys in data, we can have data having different keys in the same partition? In this case how data order is kept?
是的,您最终可能会在同一个分区中拥有不同的密钥。然后保留此特定分区的顺序,但在主题中不保证 overall.So 假设您有键 A、B 和 C 以及一个包含两个分区的主题。 A 和 C 进入第一个分区,B 存储在第二个分区中。如果数据像这样流动: A/V1、A/V2、B/V1、C/V1、B/V2
那么你的分区将被填充成这样:
- 分区 0:A/V1、A/V2、C/V1
- 分区 1:B/V1、B/V2
使用此主题时,不清楚 A 消息和 C 消息之间的顺序与 B 消息的关系。但是始终保证消息A/V1在A/V2之前消费,A/V2在C/V1之前消费,B/V1在B/V2之前消费。
如果您正在寻找一种更灵活的方式将消息定向到分区中,您还可以考虑编写 custom partitioner。