[Python] How do I produce a message with kafka-python?

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    I try to use kafka-python to produce a message.
    The error kafka.errors.KafkaTimeoutError: KafkaTimeoutError: Failed to update metadata after 60.0 secs. occurs.
    I guess something is wrong with authentication. There is no ssl on the server, but user and password authentication.
    The code is below. What should be changed to send message successfully?

    from kafka import KafkaProducer
    import json

    KAFKA_TOPIC = 'TESTTOPIC'
    KAFKA_BROKERS = 'localhost:9092'

    producer = KafkaProducer(
    bootstrap_servers=[KAFKA_BROKERS],
    sasl_plain_username='USER',
    sasl_plain_password='PASS',
    sasl_mechanism='PLAIN',
    security_protocol='PLAIN',
    value_serializer=lambda m: json.dumps(m).encode(),
    api_version=(0,10,2)
    )

    info_as_json = {'foo': 'bar'}

    producer.send(KAFKA_TOPIC, info_as_json)

    Continue reading...

Compartilhe esta Página