[英]How to subscribe to a list of multiple kafka wildcard patterns using kafka-python?
I'm subscribing to Kafka using a pattern with a wildcard, as shown below.我正在使用带有通配符的模式订阅 Kafka,如下所示。 The wildcard represents a dynamic customer id.
通配符代表动态客户 ID。
consumer.subscribe(pattern='customer.*.validations')
This works well, because I can pluck the customer Id from the topic string.这很有效,因为我可以从主题字符串中提取客户 ID。 But now I need to expand on the functionality to listen to a similar topic for a slightly different purpose.
但是现在我需要扩展功能,以便为稍微不同的目的收听类似的主题。 Let's call it
customer.*.additional-validations
.我们称之为
customer.*.additional-validations
。 The code needs to live in the same project because so much functionality is shared, but I need to be able to take a different path based on the type of queue.代码需要存在于同一个项目中,因为共享了这么多功能,但我需要能够根据队列的类型采用不同的路径。
In the Kafka documentation I can see that it is possible to subscribe to an array of topics.在Kafka 文档中,我可以看到可以订阅一系列主题。 However these are hard-coded strings.
然而,这些是硬编码的字符串。 Not patterns that allow for flexibility.
不是允许灵活性的模式。
>>> # Deserialize msgpack-encoded values
>>> consumer = KafkaConsumer(value_deserializer=msgpack.loads)
>>> consumer.subscribe(['msgpackfoo'])
>>> for msg in consumer:
... assert isinstance(msg.value, dict)
So I'm wondering if it is possible to somehow do a combination of the two?所以我想知道是否有可能以某种方式将两者结合起来? Kind of like this (non-working):
有点像这样(非工作):
consumer.subscribe(pattern=['customer.*.validations', 'customer.*.additional-validations'])
In the KafkaConsumer code, it supports list of topics, or a pattern,在 KafkaConsumer 代码中,它支持主题列表或模式,
https://github.com/dpkp/kafka-python/blob/68c8fa4ad01f8fef38708f257cb1c261cfac01ab/kafka/consumer/group.py#L717 https://github.com/dpkp/kafka-python/blob/68c8fa4ad01f8fef38708f257cb1c261cfac01ab/kafka/consumer/group.py#L717
def subscribe(self, topics=(), pattern=None, listener=None):
"""Subscribe to a list of topics, or a topic regex pattern
Partitions will be dynamically assigned via a group coordinator.
Topic subscriptions are not incremental: this list will replace the
current assignment (if there is one).
So you can create a regex, with OR condition using |
所以你可以创建一个正则表达式,使用 OR 条件
|
, that should work as subscribe to multiple dynamic topics regex, as it internally uses re
module for matching. ,这应该像订阅多个动态主题正则表达式一样工作,因为它在内部使用
re
模块进行匹配。
(customer.*.validations)|(customer.*.additional-validations)
In the Confluent Kafka library , the subscribe doesn't have a pattern
keyword but instead will process regex patterns that start with ^
.在Confluent Kafka 库中,订阅没有
pattern
关键字,而是会处理以^
开头的正则表达式模式。
def subscribe(self, topics, on_assign=None, *args, **kwargs):
"""
Set subscription to a supplied list of topics
This replaces a previous subscription.
Regexp pattern subscriptions are supported by prefixing the topic string with ``"^"``, e.g.::
consumer.subscribe(["^my_topic.*", "^another[0-9]-?[a-z]+$", "not_a_regex"])
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.