Azure function output to Event Hub partition

Avasilcai Adrian 25 Reputation points
2023-07-03T09:47:49.2266667+00:00

Hi all,

I'm trying to figure out how can I specify a certain partition index when sending a message to an Event Hub while using a Node.js function. Does anyone have any idea?

Let's just use the default Microsoft example as a starting point:

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();
    context.log('Message created at: ', timeStamp);   
    context.bindings.outputEventHubMessage = "Message created at: " + timeStamp;
    context.done();
};
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,414 questions
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
690 questions
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,836 Reputation points
    2023-07-11T05:08:04.8466667+00:00

    @Avasilcai Adrian Thanks for reaching out. You need to leverage the event hub SDK to send the event to a particular partition. Unfortunately, there is no configuration in the event hub output binding that can send the message to single partition.

    You can leverage the SDK to send the event as below. Please test it and update it as per your business need.

    module.exports = function (context, myTimer) {
        var timeStamp = new Date().toISOString();
        context.log('Message created at: ', timeStamp);
    
        const { EventHubProducerClient, EventData } = require("@azure/event-hubs");
    
        const connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING";
        const eventHubName = "EVENT HUB NAME";
        const producer = new EventHubProducerClient(connectionString, eventHubName);
    
        const eventData = {
            body: "Message created at: " + timeStamp,
            partitionKey: "cities" // Set the partitionKey to "cities" to always send the message to the same partition
        };
    
        producer.send(eventData).then(() => {
            context.log(`Message sent to partition ${producer.partitionId}`);
            context.done();
        }).catch((err) => {
            context.log(`Error sending message: ${err}`);
            context.done(err);
        });
    };
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.