Code python

Subscribe to Multiple Topics in Kafka Consumer

Kontext Kontext visibility 1,806 comment 0 access_time 2 years ago language English

descriptionCode description

When fetching records from Kafka bootstrap servers, we can specify one consumer to subscribe to more than one topic. This can be done in the init function by passing a list of topics:

topics = ['kontext-events', 'other-events']
    bootstrap_servers = 'localhost:9092'
    consumer = KafkaConsumer(
        *topics,
        client_id='local-test',
        bootstrap_servers=bootstrap_servers,
        auto_offset_reset='earliest'
    )

Alternatively, we can use subscribe() function to subscribe to multiple topics. We can also regular expressions to match topics via patterns:

subscribe(topics=(), pattern=None, listener=None)

fork_rightFork
more_vert
copyright This page is subject to Site terms.
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts