Azure 函数和应用服务的输出 JSON 数组被拆分成单独的消息
Output JSON Array from Azure function and app service being split up into individual messages
我有一个 azure 函数和一个 azure 应用程序服务,它们都表现出相同的行为。我从两者中输出一个有效载荷,其中包含一个 Json 数组和几个 Json 对象 [{"x": "y"}, {"a": "b"} ] 在它到一个 eventhub。当我检查 eventhub 时,我发现该数组已被拆分为各个组成部分 Json 对象。
{"x": "y"}
和
{"a": "b"}
(即 2 条消息)
我在任何地方都找不到这方面的文档,也不知道它是否是应用程序运行时、客户端库或事件中心本身的功能。
我希望能够控制这种行为。我找到的唯一方法是将数组包装在一个 Json 对象中,如下所示:
{"payload": [{"x": "y"}, {"a": "b"}]}
编辑 1:
函数写在java中,用spring云函数。以下是包含绑定的处理程序的定义:
public class CollectMessageHandler extends AzureSpringBootRequestHandler<Message, String> {
@FunctionName("collect")
@EventHubOutput(name = "output", eventHubName = "collected", connection = "OUT_CONN_STRING")
public String collect(
@EventHubTrigger(name = "input", eventHubName = "incoming", cardinality = Cardinality.ONE, connection = "IN_CONN_STRING") Message message,
ExecutionContext targetExecutionContext
) {
return handleRequest(message, targetExecutionContext);
}
}
我可以重现这个问题,在更改多个 dataType
和 return 类型后,它仍然将数组解析为多个 json 对象。所以我想 java Azure 函数会默认执行此操作。
所以在此之后我的解决方案是将这个 json 数组放入另一个数组,如下面的代码。
public void run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context,
@EventHubOutput(name = "event", eventHubName = "test", connection = "AzureEventHubConnection",dataType = "String") OutputBinding<Object> output) {
context.getLogger().info("Java HTTP trigger processed a request.");
JSONObject jo1 = new JSONObject();
jo1.put("firstName", "John");
jo1.put("lastName", "Doe");
JSONObject jo2 = new JSONObject();
jo2.put("firstName", "george");
jo2.put("lastName", "chen");
JSONArray ja = new JSONArray();
ja.add(jo1);
ja.add(jo2);
Object[] o=new Object[]{ja};
output.setValue(o);
}
然后在事件中它将显示为如下图所示的数组。
我有一个 azure 函数和一个 azure 应用程序服务,它们都表现出相同的行为。我从两者中输出一个有效载荷,其中包含一个 Json 数组和几个 Json 对象 [{"x": "y"}, {"a": "b"} ] 在它到一个 eventhub。当我检查 eventhub 时,我发现该数组已被拆分为各个组成部分 Json 对象。
{"x": "y"}
和
{"a": "b"}
(即 2 条消息)
我在任何地方都找不到这方面的文档,也不知道它是否是应用程序运行时、客户端库或事件中心本身的功能。
我希望能够控制这种行为。我找到的唯一方法是将数组包装在一个 Json 对象中,如下所示:
{"payload": [{"x": "y"}, {"a": "b"}]}
编辑 1: 函数写在java中,用spring云函数。以下是包含绑定的处理程序的定义:
public class CollectMessageHandler extends AzureSpringBootRequestHandler<Message, String> {
@FunctionName("collect")
@EventHubOutput(name = "output", eventHubName = "collected", connection = "OUT_CONN_STRING")
public String collect(
@EventHubTrigger(name = "input", eventHubName = "incoming", cardinality = Cardinality.ONE, connection = "IN_CONN_STRING") Message message,
ExecutionContext targetExecutionContext
) {
return handleRequest(message, targetExecutionContext);
}
}
我可以重现这个问题,在更改多个 dataType
和 return 类型后,它仍然将数组解析为多个 json 对象。所以我想 java Azure 函数会默认执行此操作。
所以在此之后我的解决方案是将这个 json 数组放入另一个数组,如下面的代码。
public void run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context,
@EventHubOutput(name = "event", eventHubName = "test", connection = "AzureEventHubConnection",dataType = "String") OutputBinding<Object> output) {
context.getLogger().info("Java HTTP trigger processed a request.");
JSONObject jo1 = new JSONObject();
jo1.put("firstName", "John");
jo1.put("lastName", "Doe");
JSONObject jo2 = new JSONObject();
jo2.put("firstName", "george");
jo2.put("lastName", "chen");
JSONArray ja = new JSONArray();
ja.add(jo1);
ja.add(jo2);
Object[] o=new Object[]{ja};
output.setValue(o);
}
然后在事件中它将显示为如下图所示的数组。