forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecv.py
More file actions
44 lines (36 loc) · 1.64 KB
/
recv.py
File metadata and controls
44 lines (36 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
"""
An example to show receiving events from an Event Hub partition.
"""
import os
import time
from azure.eventhub import EventHubClient, EventPosition, EventHubSharedKeyCredential
HOSTNAME = os.environ['EVENT_HUB_HOSTNAME'] # <mynamespace>.servicebus.windows.net
EVENT_HUB = os.environ['EVENT_HUB_NAME']
USER = os.environ['EVENT_HUB_SAS_POLICY']
KEY = os.environ['EVENT_HUB_SAS_KEY']
EVENT_POSITION = EventPosition("-1")
PARTITION = "0"
total = 0
last_sn = -1
last_offset = "-1"
client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY),
network_tracing=False)
consumer = client.create_consumer(consumer_group="$default", partition_id=PARTITION,
event_position=EVENT_POSITION, prefetch=5000)
with consumer:
start_time = time.time()
batch = consumer.receive(timeout=5)
while batch:
for event_data in batch:
last_offset = event_data.offset
last_sn = event_data.sequence_number
print("Received: {}, {}".format(last_offset, last_sn))
print(event_data.body_as_str())
total += 1
batch = consumer.receive(timeout=5)
print("Received {} messages in {} seconds".format(total, time.time() - start_time))