0

I am trying to read messages from Azure Event Hub using kafka API. But I am unable to connect to the event hub. The bootstrap_connected() function always return false and the code stops there. It is not going to print message part. Below is my code

from kafka import KafkaConsumer
from json import loads
consumer = KafkaConsumer(<EVENTHUB_NAME>,
                         bootstrap_servers='<NAME>.servicebus.windows.net:9093',
                         auto_offset_reset='earliest',
                         enable_auto_commit=True,
                         security_protocol='SASL_SSL',
                         sasl_mechanism='PLAIN',
                         sasl_plain_username=<CLIENT ID>,
                         sasl_plain_password=<CONNECTION_STRING>,
                         group_id=<CONSUMER_GROUP_NAME>,
                         api_version=(0, 11),
                         value_deserializer=lambda x: loads(x.decode('utf-8')))
print(consumer) #returns <kafka.consumer.group.KafkaConsumer object at 0x00000000012A8130>
x = consumer.bootstrap_connected() #returns False
print(x)
for message in consumer:
    message = message.value
    print('{} added'.format(message))

I have followed the documentation but not sure why it is not connecting. I am able to use Azure's EventHubConsumerClient and able to receive the message. But my business logic requires to use Kafka Consumer Client. Please do help me in finding a solution for this!

potterson11
  • 147
  • 7
  • You need to subscribe your topic then you can see the messages. – Ajay K Dec 07 '22 at 11:38
  • @AjayK As you said I have added this line - `consumer.subscribe(topics = [])`. But still no messages printed. Atleast bootstrap_connected() should have returned true but it return False. – potterson11 Dec 07 '22 at 11:43
  • @AjayK Also Kafka's Topic is equivalent to EventHub name so the above code would be redundant.. – potterson11 Dec 07 '22 at 11:45
  • If bootstrap_connected() returns False that's mean bootstrap node is not connected. – Ajay K Dec 07 '22 at 13:22

0 Answers0