Search code examples
pythonapache-pulsarpulsarpython-pulsar

How can I read all topic?


When I create the consumer

consumer = pulsar.Client(
            PULSAR_URL,
            authentication=AuthenticationOauth2(params)
        ).subscribe(
            topic=PULSAR_TOPIC,
            subscription_name=PULSAR_SUBSCRIPTION_NAME
        )

I cannot read all messages from the beginning, or all non read messages, I only can read messages created after the consumer is created.

The questions is about how can I set the consumer in order to read all non read messages previously.

Thanks


Solution

  • You can specify the initial_position in the subscribe method to set the initial position of a consumer when subscribing to the topic. It could be either: InitialPosition.Earliest or InitialPosition.Latest. Default: Latest

    So in your case, if you wanted to start at the oldest available message then you would want something like:

    consumer = pulsar.Client(
            PULSAR_URL,
            authentication=AuthenticationOauth2(params)
        ).subscribe(
            topic=PULSAR_TOPIC,
            subscription_name=PULSAR_SUBSCRIPTION_NAME,
            initial_position=InitialPosition.Earliest
        )
    

    Hope this helps!