Skip to content

Azure-Samples/azure-functions-eventhub-sdk-bindings-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

name description languages products page_type urlFragment
EventHub SDK-Type Bindings with Azure Functions (Python)
Bind to rich SDK types when using EventHub trigger.
python
azure
azure-functions
azure-eventhub
sdk-bindings
sample
eventhub-sdk-type-bindings-with-azure-functions

EventHub SDK-Type Bindings with Azure Functions (Python)

This sample demonstrates how to use the Azure Functions EventHub SDK-type bindings in Python. The supported SDK types includes EventData.

You can learn more about SDK-type bindings for EventHub in the SDK-type Bindings for Python Reference.

Prerequisites

Before running the sample, you need the following:

  1. Azure Subscription: An Azure account is required.

  2. Azure Functions Core Tools: Install Azure Functions Core Tools to run and test functions locally.

  3. Python 3.x: Ensure Python 3.9 or later is installed on your machine.

  4. Azure Storage Account: Create a storage account via the Azure Portal and get the connection string.

Using SDK-type Bindings for EventHub in an Azure Function App

The code in the sample folder has already been updated to support use of SDK-type bindings for EventHub. Let's walk through the changed files.

The requirements.txt file has an additional dependency of the azurefunctions-extensions-bindings-eventhub module:

azure-functions
azurefunctions-extensions-bindings-eventhub

The eventhub_samples_eventdata folder contains function_app.py which imports the azurefunctions-extensions-bindings-eventhub module.

import azure.functions as func
import azurefunctions.extensions.bindings.eventhub as eventhub

In each function_app.py file, there are two functions defined: both EventHub triggers. Both of these functions specify an arg named event and define the type as an SDK-type.

The eventhub_samples_eventdata directory shows the type defined as EventData.

@app.event_hub_message_trigger(
    arg_name="event", event_hub_name="EVENTHUB_NAME", connection="EventHubConnection"
)
def eventhub_trigger(event: eh.EventData):

Running the sample

Testing Locally

  1. Clone the repository:
        git clone https://github.com/Azure-Samples/azure-functions-eventhub-sdk-bindings-python.git
    
  2. Navigate to the project directory:
        cd eventhub_samples_eventdata
    
  3. Create a Python virtual environment and activate it.
  4. Install the required dependencies:
        pip install -r requirements.txt
    
  5. Update local.settings.json: replace EventHubConnection with your desired EventHub connection string.
  6. Update the EventHub name: in function_app.py, replace EVENTHUB_NAME with your EventHub name.
  7. Start the function: If you are using VS Code for development, click the "Run and Debug" button or follow the instructions for running a function locally. Outside of VS Code, follow these instructions for using Core Tools commands directly to run the function locally.
        Functions:
                eventhub_trigger: eventHubTrigger
    
  8. Execute the function:
    • EventHub trigger: upload an event to the EventHub instance in any way (such as using an EventHub output function). You should see the log below printed in the terminal, where the EventHub message that was sent is printed as a string.
       Python EventHub trigger processed an event { ... }
    

Deploying to Azure

There are three main ways to deploy this to Azure:

All approaches will provision a Function App, Storage account (to store the code), and a Log Analytics workspace.

Next Steps

Visit the SDK-type bindings in Python reference documentation to learn more about how to use SDK-type bindings in a Python Function App and the API reference documentation to learn more about what you can do with the Azure EventHub library.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy