azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

EventHub output binding: set partitionKey for output event

Open tartieret opened this issue 5 years ago • 7 comments

Context

The EventHub SDK allows to specify the partitionKey for a batch of events, as shown here:

batch = hub_client.create_batch(partition_key=mykey)
batch.add(EventData(json.dumps({ ... })))
hub_client.send_batch(batch)

Issue

The documentation for Azure Functions shows how to use a function return value to send an event to EventHub, using the following configuration and function:

{
    "type": "eventHub",
    "name": "$return",
    "eventHubName": "myeventhub",
    "connection": "MyEventHubSendAppSetting",
    "direction": "out"
}
import logging
import azure.functions as func

def main(timer: func.TimerRequest) -> str:
    return 'Hello world'

How can we modify the example above to set the partitionKey on the output message ?

tartieret avatar Apr 17 '20 18:04 tartieret

Hey @tartieret. This is not possible today. Are you able to bring in the Event Hubs SDK instead of using the binding?

anthonychu avatar May 08 '20 20:05 anthonychu

Hi @anthonychu I tried using the Event Hub SDK directly in an Azure Function, but the issue is that the function has to initiate a new connection at each execution, and this is quite slow, so the function regularly times out.

The challenge is that the data has to be partitionned a certain way to allow a "parallel" query in Stream Analytics (per "sensor" in my IOT scenario). However, my system is receiving data "per device" (where we have several sensors connected to the same device/gateway), so I have to re-partition the data. I was hoping to do that with an Azure Function using event hub input and output bindings.

tartieret avatar May 08 '20 21:05 tartieret

Perhaps this is the wrong thread, but the context seems relevant.

In the example shown, @tartieret uses batch = client.creatBatch(). I'd like to enhance some code to remove dependency on the client and simply use output bindings. How can I take advantage of batch processing without using the eh client?

jbevarts avatar Jun 03 '20 04:06 jbevarts

@jbevarts Looks like the output binding should take a list. @Hazhzeng Can you confirm the behavior? Perhaps docs need updating, as the JavaScript example shows how to return a batch.

@tartieret Sorry for the delay. You might be able to create the Event Hubs client once and store it in a global variable, reusing it across different function invocations.

Looks like in order to supporting outputting a partition key, we'd have to make significant modifications to the Event Hubs extension. @Hazhzeng do you know if this is true?

anthonychu avatar Jun 16 '20 16:06 anthonychu

Thanks @anthonychu We found a work-around without using an Azure Function, so it's not critical for me anymore.

tartieret avatar Jun 17 '20 21:06 tartieret

Any update on this?

barbara-desousa-kroger avatar Jun 02 '22 21:06 barbara-desousa-kroger

I'd also be interested in utilizing this functionality!

cgreco-cm avatar Oct 19 '22 19:10 cgreco-cm