Search code examples
javaazureazure-functionsazure-eventhubazure-java-tools

Is there a way to output events with header data to Azure Event Hubs using Azure Functions in Java?


This question asks how to get event header data into the Function and the accepted answer says that there is a way.

How about the other side - is it possible to output events with header data? In the docs I found only how to do it in C#.


Solution

  • I don't know of a way to do this with the output bindings; to my knowledge, those only support native Java types or a POJO. (ref)

    However, it is possible to use the Java SDK directly in the body of the Function which would give you direct control over forming the EventData instance to be published - including the Properties collection and other metadata.

    With respect to copying data, if you're looking to ensure that you've got an exact replica (other than broker-owned metadata) - you'll want to publish to the exact partition that you've read from. (see sample below)

    Ensuring the partition key could be done a couple of different ways, depending on the configuration of the Event Hub replica. For the simple approach, just publishing using the partition key should ensure that the event ends up in the same partition on the replica - assuming that the number of partitions match exactly. (see sample below)

    If your replica has a different number of partitions, you'll need to publish to the partition directly and manipulate the underlying AMQP Message to inject the partition key into the Message Annotations section manually by adding an item with the key x-opt-partition-key and value of the partition key that you'd like it to reflect. To do this, you'll need to use the EventData.getRawAmqpMessage method.

    For more information: