radish25
radish25

Reputation: 75

Azure Event Hub Python SDK

Can anyone explain how to use the "Event Hub-compatible name" and "Event Hub-Compatible endpoint" using the python event hub SDK. The SDK asks for

# Address can be in either of these formats:
# "amqps://<URL-encoded-SAS-policy>:<URL-encoded-SAS- 
# "key>@<mynamespace>.servicebus.windows.net/myeventhub"
# "amqps://<mynamespace>.servicebus.windows.net/myeventhub"
ADDRESS = os.environ.get('EVENT_HUB_ADDRESS')

# SAS policy and key are not required if they are encoded in the URL
USER = os.environ.get('EVENT_HUB_SAS_POLICY')
KEY = os.environ.get('EVENT_HUB_SAS_KEY')

I need help fitting the strings given under the default endpoint into this example.

Upvotes: 1

Views: 723

Answers (2)

Michael Xu
Michael Xu

Reputation: 4432

If you want to receive the device-to-cloud messages from IoT Hub built-in endpoint with the latest Azure Event Hub Python SDK, there is another option, you can refer to following code:

import os
import sys
import logging
import time
from azure.eventhub import EventHubClient, Receiver, Offset

PARTITION = "0"

total = 0
last_sn = -1
last_offset = "-1"

client = EventHubClient.from_iothub_connection_string("{iot hub connection string}", debug=True)

try:
    receiver = client.add_receiver("$default", PARTITION, operation='/messages/events')
    client.run()
    start_time = time.time()
    for event_data in receiver.receive(timeout=100):
        last_offset = event_data.offset
        last_sn = event_data.sequence_number
        print("Received: {}, {}".format(last_offset, last_sn))
        total += 1

    end_time = time.time()
    client.stop()
    run_time = end_time - start_time
    print("Received {} messages in {} seconds".format(total, run_time))

except KeyboardInterrupt:
    pass
finally:
    client.stop()

I think if there is no particular reason,it is better to use IoT Hub own specific Python client library to receive the messages.

Upvotes: 0

Dominic Betts
Dominic Betts

Reputation: 2331

Given that you mention "Event Hub-compatible name" and "Event Hub-Compatible endpoint", I assume you are trying to connect to an IoT hub's Event Hub-compatible endpoint.

With this Event Hub-compatible name: iothub-ehub-getstarted-99999-xxxxxxxxxx and this Event Hub-Compatible endpoint: Endpoint=sb://ihsuprodbyres999dednamespace.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=

Then the EVENT_HUB_ADDRESS looks like this: amqps://ihsuprodbyres999dednamespace.servicebus.windows.net/iothub-ehub-getstarted-99999-xxxxxxxxxx

For EVENT_HUB_SAS_POLICY and EVENT_HUB_SAS_KEY values, you can use either the iothubownwer or service as the SAS policy along with it's corresponding key. You can find these in the portal on your IoT hub's Shared access policies page.

Upvotes: 1

Related Questions