主题、分区和键 [英] Topics, partitions and keys

查看:25
本文介绍了主题、分区和键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关该主题的一些说明.在 Kafka 文档中,我发现了以下内容:

I am looking for some clarification on the subject. In Kafka documentations I found the following:

Kafka 仅提供分区内消息的总顺序,而不提供主题中不同分区之间的总顺序.对于大多数应用程序来说,按分区排序与按键分区数据的能力相结合就足够了.但是,如果您需要消息的总顺序,这可以通过只有一个分区的主题来实现,尽管这意味着每个消费者组只有一个消费者进程.

Kafka only provides a total order over messages within a partition, not between different partitions in a topic. Per-partition ordering combined with the ability to partition data by key is sufficient for most applications. However, if you require a total order over messages this can be achieved with a topic that has only one partition, though this will mean only one consumer process per consumer group.

这里是我的问题:

  1. 这是否意味着如果我希望有 1 个以上的消费者(来自同一组)阅读一个主题,我需要有 1 个以上的分区?

  1. Does it mean if i want to have more than 1 consumer (from the same group) reading from one topic I need to have more than 1 partition?

这是否意味着我需要与同一组消费者数量相同的分区数量?

Does it mean I need same amount of partitions as amount of consumers for the same group?

一个分区可以读取多少消费者?

How many consumers can read from one partition?

关于 API 的 key 和 partition 之间的关系也有一些问题.我只看过 .net API(尤其是来自 MS 的 API),但看起来像模仿 Java API.我看到使用生产者向主题发送消息时有一个关键参数.但是当消费者从一个主题中读取时,会有一个分区号.

Also have some questions regarding relationship between keys and partitions with regard to API. I only looked at .net APIs (especially one from MS) but looks like the mimic Java API. I see when using a producer to send a message to a topic there is a key parameter. But when consumer reads from a topic there is a partition number.

  1. 分区是如何编号的?从 0 还是 1 开始?
  2. 键和分区之间究竟有什么关系?据我了解,键上的某些功能将确定一个分区.对吗?
  3. 如果我在一个主题中有 2 个分区,并且希望某些特定消息发送到一个分区而其他消息发送到另一个分区,我应该将特定键用于一个特定分区,其余的用于另一个分区?
  4. 如果我有 3 个分区,一种类型的消息发送到一个特定的分区,其余的发送到另外 2 个会怎样?
  5. 一般来说,我如何将消息发送到特定分区,以便让消费者知道从哪里读取?还是我最好有多个主题?

提前致谢.

推荐答案

分区增加了 Kafka 主题的并行性.任意数量的消费者/生产者都可以使用相同的分区.由应用层来定义协议.Kafka 保证交付.关于 API,您可能需要查看 Java 文档,因为它们可能更完整.根据我的经验:

Partitions increase parallelism of Kafka topic. Any number of consumers/producers can use the same partition. Its up to application layer to define the protocol. Kafka guarantees delivery. Regarding the API, you may want to look at Java docs as they may be more complete. Based on my experience:

  1. 分区从 0 开始
  2. 密钥可用于向同一分区发送消息.例如 hash(key)%num_partition.该逻辑可插入到 Producer 中.https://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/Partitioner.html
  3. 是的.但要小心不要以一些会导致专用"的键结束.划分.为此,您可能需要专门的主题.例如控制主题和数据主题
  4. 这似乎是与第 3 题相同的问题.
  5. 我认为消费者不应根据分区对数据做出假设.典型的方法是让消费者组可以从一个主题的多个分区中读取数据.如果您想拥有专用频道,最好(更安全/可维护)使用单独的主题.

这篇关于主题、分区和键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆