@@ -118,27 +118,25 @@ The following sections provide several code snippets covering some of the most c
118118
119119Get the partition ids of an Event Hub.
120120
121- <!-- SNIPPET:connection_string_authentication.connection_string_authentication -->
122-
123121``` python
124122import os
125123from azure.eventhub import EventHubConsumerClient
124+ from azure.identity import DefaultAzureCredential
126125
127- CONNECTION_STR = os.environ[" EVENT_HUB_CONN_STR " ]
126+ FULLY_QUALIFIED_NAMESPACE = os.environ[" EVENT_HUB_HOSTNAME " ]
128127EVENTHUB_NAME = os.environ[' EVENT_HUB_NAME' ]
129128
130- consumer_client = EventHubConsumerClient.from_connection_string (
131- conn_str = CONNECTION_STR ,
129+ consumer_client = EventHubConsumerClient(
130+ fully_qualified_namespace = FULLY_QUALIFIED_NAMESPACE ,
132131 consumer_group = ' $Default' ,
133132 eventhub_name = EVENTHUB_NAME ,
133+ credential = DefaultAzureCredential(),
134134)
135135
136136with consumer_client:
137137 pass # consumer_client is now ready to be used.
138138```
139139
140- <!-- END SNIPPET -->
141-
142140### Publish events to an Event Hub
143141
144142Use the ` create_batch ` method on ` EventHubProducerClient ` to create an ` EventDataBatch ` object which can then be sent using the ` send_batch ` method.
@@ -165,11 +163,17 @@ the `EventHubConsumerClient.receive` method will be of use as follows:
165163``` python
166164import logging
167165from azure.eventhub import EventHubConsumerClient
166+ from azure.identity import DefaultAzureCredential
168167
169- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
168+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
170169consumer_group = ' << CONSUMER GROUP >>'
171170eventhub_name = ' << NAME OF THE EVENT HUB >>'
172- client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name = eventhub_name)
171+ client = EventHubConsumerClient(
172+ fully_qualified_namespace = fully_qualified_namespace,
173+ eventhub_name = eventhub_name,
174+ consumer_group = consumer_group,
175+ credential = DefaultAzureCredential(),
176+ )
173177
174178logger = logging.getLogger(" azure.eventhub" )
175179logging.basicConfig(level = logging.INFO )
@@ -195,11 +199,17 @@ triggers the callback on a batch of events, attempting to receive a number at a
195199``` python
196200import logging
197201from azure.eventhub import EventHubConsumerClient
202+ from azure.identity import DefaultAzureCredential
198203
199- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
204+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
200205consumer_group = ' << CONSUMER GROUP >>'
201206eventhub_name = ' << NAME OF THE EVENT HUB >>'
202- client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name = eventhub_name)
207+ client = EventHubConsumerClient(
208+ fully_qualified_namespace = fully_qualified_namespace,
209+ eventhub_name = eventhub_name,
210+ consumer_group = consumer_group,
211+ credential = DefaultAzureCredential(),
212+ )
203213
204214logger = logging.getLogger(" azure.eventhub" )
205215logging.basicConfig(level = logging.INFO )
@@ -223,11 +233,11 @@ Use the `create_batch` method on `EventHubProducer` to create an `EventDataBatch
223233Events may be added to the ` EventDataBatch ` using the ` add ` method until the maximum batch size limit in bytes has been reached.
224234``` python
225235import asyncio
226- from azure.eventhub.aio import EventHubProducerClient # The package name suffixed with ".aio" for async
236+ from azure.eventhub.aio import EventHubProducerClient # The package name suffixed with ".aio" for async
227237from azure.eventhub import EventData
238+ from azure.identity.aio import DefaultAzureCredential
228239
229- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
230- consumer_group = ' << CONSUMER GROUP >>'
240+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
231241eventhub_name = ' << NAME OF THE EVENT HUB >>'
232242
233243async def create_batch (client ):
@@ -241,7 +251,11 @@ async def create_batch(client):
241251 return event_data_batch
242252
243253async def send ():
244- client = EventHubProducerClient.from_connection_string(connection_str, eventhub_name = eventhub_name)
254+ client = EventHubProducerClient(
255+ fully_qualified_namespace = fully_qualified_namespace,
256+ eventhub_name = eventhub_name,
257+ credential = DefaultAzureCredential(),
258+ )
245259 batch_data = await create_batch(client)
246260 async with client:
247261 await client.send_batch(batch_data)
@@ -260,8 +274,9 @@ aio, one would need the following:
260274import logging
261275import asyncio
262276from azure.eventhub.aio import EventHubConsumerClient
277+ from azure.identity.aio import DefaultAzureCredential
263278
264- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
279+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
265280consumer_group = ' << CONSUMER GROUP >>'
266281eventhub_name = ' << NAME OF THE EVENT HUB >>'
267282
@@ -273,7 +288,12 @@ async def on_event(partition_context, event):
273288 await partition_context.update_checkpoint(event)
274289
275290async def receive ():
276- client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name = eventhub_name)
291+ client = EventHubConsumerClient(
292+ fully_qualified_namespace = fully_qualified_namespace,
293+ eventhub_name = eventhub_name,
294+ consumer_group = consumer_group,
295+ credential = DefaultAzureCredential(),
296+ )
277297 async with client:
278298 await client.receive(
279299 on_event = on_event,
@@ -296,8 +316,9 @@ the same within asyncio as follows:
296316import logging
297317import asyncio
298318from azure.eventhub.aio import EventHubConsumerClient
319+ from azure.identity.aio import DefaultAzureCredential
299320
300- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
321+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
301322consumer_group = ' << CONSUMER GROUP >>'
302323eventhub_name = ' << NAME OF THE EVENT HUB >>'
303324
@@ -309,7 +330,12 @@ async def on_event_batch(partition_context, events):
309330 await partition_context.update_checkpoint()
310331
311332async def receive_batch ():
312- client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name = eventhub_name)
333+ client = EventHubConsumerClient(
334+ fully_qualified_namespace = fully_qualified_namespace,
335+ eventhub_name = eventhub_name,
336+ consumer_group = consumer_group,
337+ credential = DefaultAzureCredential(),
338+ )
313339 async with client:
314340 await client.receive_batch(
315341 on_event_batch = on_event_batch,
@@ -351,11 +377,12 @@ import asyncio
351377
352378from azure.eventhub.aio import EventHubConsumerClient
353379from azure.eventhub.extensions.checkpointstoreblobaio import BlobCheckpointStore
380+ from azure.identity.aio import DefaultAzureCredential
354381
355- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
382+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
356383consumer_group = ' << CONSUMER GROUP >>'
357384eventhub_name = ' << NAME OF THE EVENT HUB >>'
358- storage_connection_str = ' << CONNECTION STRING FOR THE STORAGE >>'
385+ blob_account_url = ' << STORAGE ACCOUNT URL >>'
359386container_name = ' <<NAME OF THE BLOB CONTAINER>>'
360387
361388async def on_event (partition_context , event ):
@@ -369,11 +396,16 @@ async def receive(client):
369396 )
370397
371398async def main ():
372- checkpoint_store = BlobCheckpointStore.from_connection_string(storage_connection_str, container_name)
373- client = EventHubConsumerClient.from_connection_string(
374- connection_str,
375- consumer_group,
399+ checkpoint_store = BlobCheckpointStore(
400+ blob_account_url = blob_account_url,
401+ container_name = container_name,
402+ credential = DefaultAzureCredential()
403+ )
404+ client = EventHubConsumerClient(
405+ fully_qualified_namespace = fully_qualified_namespace,
376406 eventhub_name = eventhub_name,
407+ credential = DefaultAzureCredential(),
408+ consumer_group = consumer_group,
377409 checkpoint_store = checkpoint_store, # For load balancing and checkpoint. Leave None for no load balancing
378410 )
379411 async with client:
@@ -464,16 +496,24 @@ $ pip install uamqp
464496
465497``` python
466498from azure.eventhub import EventHubProducerClient, EventHubConsumerClient
499+ from azure.identity import DefaultAzureCredential
467500
468- connection_str = ' << CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
501+ fully_qualified_namespace = ' << EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
469502consumer_group = ' << CONSUMER GROUP >>'
470503eventhub_name = ' << NAME OF THE EVENT HUB >>'
471504
472- client = EventHubProducerClient.from_connection_string(
473- connection_str, eventhub_name = eventhub_name, uamqp_transport = True
505+ client = EventHubProducerClient(
506+ fully_qualified_namespace = fully_qualified_namespace,
507+ eventhub_name = eventhub_name,
508+ credential = DefaultAzureCredential(),
509+ uamqp_transport = True
474510)
475- client = EventHubConsumerClient.from_connection_string(
476- connection_str, consumer_group, eventhub_name = eventhub_name, uamqp_transport = True
511+ client = EventHubConsumerClient(
512+ fully_qualified_namespace = fully_qualified_namespace,
513+ eventhub_name = eventhub_name,
514+ credential = DefaultAzureCredential(),
515+ consumer_group = consumer_group,
516+ uamqp_transport = True
477517)
478518```
479519
0 commit comments