问题描述
我是Kafka Java API的新手,我正在使用来自特定Kafka主题的记录.
I am new with Kafka Java API and I am working on consuming records from a particular Kafka topic.
我知道我可以使用方法subscribe()
从该主题开始轮询记录.如果我要从主题的选定分区中开始轮询记录,则Kafka还提供方法assign()
.
I understand that I can use method subscribe()
to start polling records from the topic. Kafka also provides method assign()
if I want to start polling records from selected partitions of the topics.
我想知道这是否是两者之间的唯一区别吗?
I want to understand if this is the only difference between the two?
推荐答案
是的subscribe
需要group.id
,因为组中的每个使用者都会动态分配给分区,以获取订阅方法中提供的主题列表,并且可以使用每个分区由该组中的一个消费者线程进行.这是通过平衡使用者组中所有成员之间的分区来实现的,以便将每个分区分配给组中的一个使用者.
Yes subscribe
need group.id
because each consumer in a group will dynamically assigned to partitions for list of topics provided in subscribe method and each partition can be consumed by one consumer thread in that group. This is achieved by balancing the partitions between all members in the consumer group so that each partition is assigned to exactly one consumer in the group
assign
将为此用户手动分配分区列表.并且此方法不使用使用者的组管理功能(无需group.id
)
assign
will manually assign a list of partitions to this consumer. and this method does not use the consumer's group management functionality (where no need of group.id
)
主要区别是assign(Collection)
将使控制器失去动态分区分配和使用者组协调的作用
The main difference is assign(Collection)
will loose the controller over dynamic partition assignment and consumer group coordination
订阅
public void subscribe(java.util.Collection<java.lang.String> topics)
subscribe方法订阅给定的主题列表以获取动态分配的分区.如果给定的主题列表为空,则将其与unsubscribe().
The subscribe method Subscribe to the given list of topics to get dynamically assigned partitions. and if the given list of topics is empty, it is treated the same as unsubscribe().
Number of partitions change for any of the subscribed list of topics
Topic is created or deleted
An existing member of the consumer group dies
A new member is added to an existing consumer group via the join API
分配
public void assign(java.util.Collection<TopicPartition> partitions)
assign方法将分区列表手动分配给该使用者.如果给定的主题分区列表为空,则将其与unsubscribe()相同.
The assign method manually assign a list of partitions to this consumer. And if the given list of topic partitions is empty, it is treated the same as unsubscribe().
这篇关于KafkaConsumer Java API Subscribe()与Assign()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!