KafkaConsumer Java API Subscribe()与Assign() [英] KafkaConsumer Java API subscribe() vs assign()

查看:780
本文介绍了KafkaConsumer Java API Subscribe()与Assign()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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

使用者也可以使用assign(Collection)手动分配特定分区(类似于较早的简单"使用者). 在这种情况下,动态分区分配和使用者组协调将被禁用.

订阅

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().

作为组管理的一部分,消费者将跟踪属于特定组的消费者列表,并且如果以下事件之一触发,则会触发重新平衡操作-

As part of group management, the consumer will keep track of the list of consumers that belong to a particular group and will trigger a rebalance operation if one of the following events trigger -

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().

通过此方法进行的手动主题分配不使用使用者的组管理功能.这样,当组成员身份或群集和主题元数据发生更改时,将不会触发任何重新平衡操作.

Manual topic assignment through this method does not use the consumer's group management functionality. As such, there will be no rebalance operation triggered when group membership or cluster and topic metadata change.

这篇关于KafkaConsumer Java API Subscribe()与Assign()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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