我有一个iot-hub
,它同时接收JSON和非JSON(十六进制)消息。这些全部都转到我的Java函数应用中进行解码。基于device-id
,我正在调用其他解码器。
我正在尝试获取所收到消息的实际iothub-connection-device-id
。
public class TranslateEndpoint {
/**
* This function will be invoked when an event is received from Event Hub.
*/
@FunctionName("TranslateEndpoint")
public void run(
@EventHubTrigger(name = "message", eventHubName = "NAME-DeviceIntegration", connection = "HostName=HOST;SharedAccessKeyName=NAME;SharedAccessKey=KEY=", consumerGroup = "$Default", cardinality = Cardinality.ONE) EventData message,
final ExecutionContext context
) {
context.getLogger().info("Java Event Hub trigger function executed.");
context.getLogger().info("Length:" + message.toString());
TranslateController temp = new TranslateController();
// Build up a list with all the data
context.getLogger().info(message.getSystemProperties().getPublisher());
context.getLogger().info(message.getSystemProperties().getPartitionKey());
context.getLogger().info(message.getSystemProperties().get("iothub-connection-device-id").toString());
}
上面的代码受我发现的一些C#代码启发。不幸的是我得到一个错误
Stack: java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.microsoft.azure.eventhubs.EventData. Registering an InstanceCreator with Gson for this type may fix this problem.
在我使用String
之前,只得到了我的实际有效载荷。接收邮件的系统属性的正确方法是什么?
答案 0 :(得分:0)
至少在Java中,元数据属性必须通过附加的带注释的参数提取,在这种情况下:
children
然后可以从该参数中检索设备ID:
div
用//The system properties, including the event data
@BindingName("SystemProperties") Map<String, Object> systemProperties
注释的String deviceId = (String) systemProperties.get("iothub-connection-device-id");
参数应该是字符串或字节数组。
在这种情况下,无法映射POJO(或message
),因为后备数据仅包含事件有效负载/值。
因此,该函数应如下所示:
@EventHubTrigger
文档:
事件元数据绑定:https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-java#metadata
系统属性:https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-construct
我忍不住说说,文档可能不那么麻烦,就像所有受支持语言中的API可能更加一致一样。我可能仍会缺少一些东西。