Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update sample codes and docstring (#6643)
  • Loading branch information
yunhaoling authored Aug 2, 2019
commit 97dfce513a3c62de6474dca9c7eea1698cf8a92a
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):

def _check_closed(self):
if self.error:
raise EventHubError("{} has been closed. Please create a new consumer to receive event data.".format(self.name))
raise EventHubError("{} has been closed. Please create a new one to handle event data.".format(self.name))

def _create_handler(self):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):

def _check_closed(self):
if self.error:
raise EventHubError("{} has been closed. Please create a new consumer to receive event data.".format(self.name))
raise EventHubError("{} has been closed. Please create a new one to handle event data.".format(self.name))

def _create_handler(self):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ async def create_batch(self, max_size=None, partition_key=None):
:type partition_key: str
:return: an EventDataBatch instance
:rtype: ~azure.eventhub.EventDataBatch

Example:
.. literalinclude:: ../examples/async_examples/test_examples_eventhub_async.py
:start-after: [START eventhub_client_async_create_batch]
:end-before: [END eventhub_client_async_create_batch]
:language: python
:dedent: 4
:caption: Create EventDataBatch object within limited size

"""

if not self._max_message_size_on_link:
Expand Down Expand Up @@ -195,7 +204,7 @@ async def send(self, event_data, *, partition_key=None, timeout=None):
:rtype: None

Example:
.. literalinclude:: ../examples/test_examples_eventhub.py
.. literalinclude:: ../examples/async_examples/test_examples_eventhub_async.py
:start-after: [START eventhub_client_async_send]
:end-before: [END eventhub_client_async_send]
:language: python
Expand Down
9 changes: 9 additions & 0 deletions sdk/eventhub/azure-eventhubs/azure/eventhub/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ def create_batch(self, max_size=None, partition_key=None):
:type partition_key: str
:return: an EventDataBatch instance
:rtype: ~azure.eventhub.EventDataBatch

Example:
.. literalinclude:: ../examples/test_examples_eventhub.py
:start-after: [START eventhub_client_sync_create_batch]
:end-before: [END eventhub_client_sync_create_batch]
:language: python
:dedent: 4
:caption: Create EventDataBatch object within limited size

"""

if not self._max_message_size_on_link:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def test_example_eventhub_async_send_and_receive(live_eventhub_config):
os.environ['EVENT_HUB_HOSTNAME'],
os.environ['EVENT_HUB_SAS_POLICY'],
os.environ['EVENT_HUB_SAS_KEY'],
os.environ['EVENT_HUB_NAME'])
os.environ['EVENT_HUB_NAME'])
client = EventHubClient.from_connection_string(connection_str)
# [END create_eventhub_client_async]

Expand All @@ -49,6 +49,17 @@ async def test_example_eventhub_async_send_and_receive(live_eventhub_config):

await consumer.receive(timeout=1)

# [START eventhub_client_async_create_batch]
event_data_batch = await producer.create_batch(max_size=10000)
while True:
try:
event_data_batch.try_add(EventData('Message inside EventBatchData'))
except ValueError:
# The EventDataBatch object reaches its max_size.
# You can send the full EventDataBatch object and create a new one here.
break
# [END eventhub_client_async_create_batch]

# [START eventhub_client_async_send]
async with producer:
event_data = EventData(b"A single event")
Expand Down
64 changes: 64 additions & 0 deletions sdk/eventhub/azure-eventhubs/examples/event_data_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/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 creating and sending EventBatchData within limited size.
"""

# pylint: disable=C0111

import logging
import time
import os

from azure.eventhub import EventHubClient, EventData, EventHubSharedKeyCredential

import examples
logger = examples.get_logger(logging.INFO)


HOSTNAME = os.environ.get('EVENT_HUB_HOSTNAME') # <mynamespace>.servicebus.windows.net
EVENT_HUB = os.environ.get('EVENT_HUB_NAME')

USER = os.environ.get('EVENT_HUB_SAS_POLICY')
KEY = os.environ.get('EVENT_HUB_SAS_KEY')


def create_batch_data(producer):
event_data_batch = producer.create_batch(max_size=10000)
while True:
try:
event_data_batch.try_add(EventData('Message inside EventBatchData'))
except ValueError:
# EventDataBatch object reaches max_size.
# New EventDataBatch object can be created here to send more data
break
return event_data_batch


try:
if not HOSTNAME:
raise ValueError("No EventHubs URL supplied.")

client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY),
network_tracing=False)
producer = client.create_producer()

try:
start_time = time.time()
with producer:
event_data_batch = create_batch_data(producer)
producer.send(event_data_batch)
except:
raise
finally:
end_time = time.time()
run_time = end_time - start_time
logger.info("Runtime: {} seconds".format(run_time))

except KeyboardInterrupt:
pass
11 changes: 11 additions & 0 deletions sdk/eventhub/azure-eventhubs/examples/test_examples_eventhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ def test_example_eventhub_sync_send_and_receive(live_eventhub_config):
event_data = EventData(body=list_data)
# [END create_event_data]

# [START eventhub_client_sync_create_batch]
event_data_batch = producer.create_batch(max_size=10000)
while True:
try:
event_data_batch.try_add(EventData('Message inside EventBatchData'))
except ValueError:
# The EventDataBatch object reaches its max_size.
# You can send the full EventDataBatch object and create a new one here.
break
# [END eventhub_client_sync_create_batch]

# [START eventhub_client_sync_send]
with producer:
event_data = EventData(b"A single event")
Expand Down