Is there a way to get the headers data of events (EventHub) using @EventHubTrigger of Azure Functions in Java?

huangapple go评论83阅读模式
英文:

Is there a way to get the headers data of events (EventHub) using @EventHubTrigger of Azure Functions in Java?

问题

我有一个使用azure-functions-java-library实现的Azure函数,该函数从一个EventHub接收事件,我正在使用@EventHubTrigger,问题是我需要从事件中获取头数据,但我没有看到任何获取它的方式,我已经阅读了文档,但没有找到相关信息。我需要这样做的原因是因为我从EventHub接收到具有不同Avro模式的事件,所以我需要区分它们以便进行解析。

我真的非常感谢您的帮助。

英文:

I have an Azure Function implemented with azure-functions-java-library that receives events from one EventHub and I'm using @EventHubTrigger, the problem is that I need the header data from an event but I don't see any way to get this, I have already read the docs and nothing. The reason I need this is because from the EventHub I'am receiving events with different Avro schemas so I need to distinguish them in order to parse it.

I'd really appreciate some help.

答案1

得分: 7

是的,您可以通过在方法参数上添加@BindingName("Properties")注释来检索消息元数据,如下所示。需要注意的是,您可以使用绑定表达式绑定事件的任何元数据。在这种情况下,它是"Properties"。此外,基数应为ONE。

@FunctionName("EventHubExample")
public void logEventHubMessage(
    @EventHubTrigger(name = "message", eventHubName = "test", connection = "AzureEventHubConnection", consumerGroup = "$Default", cardinality = Cardinality.ONE, dataType = "string") 
    String message,
    final ExecutionContext context,
    @BindingName("Properties")
    Map<String, Object> properties) {            
    context.getLogger().info("Event hub message received: " + message + ", properties: " + properties);
}

我使用了Service Bus Explorer作为事件发送器,以以下方式设置了事件的元数据,并且能够在消费者端使用上述代码中的"Properties"绑定来查看它们。
Is there a way to get the headers data of events (EventHub) using @EventHubTrigger of Azure Functions in Java?

注意:C#函数的SDK在这方面优于Java。在C#中,您可以获取整个事件对象,直接导航到元数据更容易,同时获取多个输入事件。但不幸的是,Java SDK目前不支持这一点,您必须单独绑定单个基数。

英文:

Yes, you can retrieve message metadata by adding a @BindingName(&quot;Properties&quot;) annotation to a method parameter like below for example. Things to note here you can bind to any metadata of an event using binding expression. In this case, it's "Properties". Also, Cardinality should be ONE.

@FunctionName(&quot;EventHubExample&quot;)
    public void logEventHubMessage(
        @EventHubTrigger(name = &quot;message&quot;, eventHubName = &quot;test&quot;, connection = &quot;AzureEventHubConnection&quot;, consumerGroup = &quot;$Default&quot;, cardinality = Cardinality.ONE, dataType = &quot;string&quot;) 
        String message,
        final ExecutionContext context,
        @BindingName(&quot;Properties&quot;)
        Map&lt;String, Object&gt; properties) {            
        context.getLogger().info(&quot;Event hub message received: &quot; + message + &quot;, properties: &quot; + properties);
    }

I used Service Bus Explorer as Event Sender to set metadata of the event as below and was able to see those in the consumer side using above code in "Properties" binding.
Is there a way to get the headers data of events (EventHub) using @EventHubTrigger of Azure Functions in Java?

N.B. C# function SDK has a benefit here over Java. In C#, you can get the whole Event object which is easier to navigate for metadata directly while getting multiple events in input. But unfortunately, that's not possible in Java SDK as of now where you have to bind separately with single cardinality.

huangapple
  • 本文由 发表于 2020年9月3日 07:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63714718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定