Wso2 流-将类似事件数据合并到一个事件中

Wso2 流-将类似事件数据合并到一个事件中,wso2,siddhi,Wso2,Siddhi,我想根据其中一个字段将传入事件合并为一个事件 输入事件: { ID: '123', eventType: 'a', eventCode: 1 }, { ID: '123', eventType: 'b', eventCode: 2 }, { ID: '123', eventType: 'c', eventCode: 3 } 预期产出: { ID: '123', events: [{ eventType: 'a', eventCode:

我想根据其中一个字段将传入事件合并为一个事件

输入事件:

{
  ID: '123',
  eventType: 'a',
  eventCode: 1
},
{
  ID: '123',
  eventType: 'b',
  eventCode: 2
},
{
  ID: '123',
  eventType: 'c',
  eventCode: 3
}
预期产出:

{
  ID: '123',
  events: [{
    eventType: 'a',
    eventCode: 1
  },
  {
    eventType: 'b',
    eventCode: 2
  },
  {
    eventType: 'c',
    eventCode: 3
  }]
}
我根据4个窗口对事件进行分组。因此,我需要处理4个事件,合并它们并将其传递到下一步

用例: 我希望使用生成的输出存储在MongoDB中,或者将其传递到外部服务

使用Siddhi可以吗


注意:我看到已经有人问了一个问题,但答复是5年前的,从那时起,Siddhi已经走了很长一段路。

您可以使用下面的Siddhi应用程序来满足您的需求。我曾经这样做过。但请注意,生成的输出正是您请求的输出。如果您想要一个正确的JSON输出,您可能还必须利用它。有关扩展使用的详细信息,请参阅自述文件

@App:name("testJsonConcat")
@App:description("Description of the plan")

-- Please refer to https://docs.wso2.com/display/SP400/Quick+Start+Guide on getting started with SP editor. 

define stream inputStream(id string, eventType string, eventCode int);

partition with (id of inputStream)
begin
from inputStream
select id, str:concat("{eventType: '", eventType, "' , eventCode :",eventCode,"}") as jsonString
insert into #formattedStream;

from #formattedStream#window.lengthBatch(4)
select str:concat("{ ID : '", id, "',events: [", str:groupConcat(jsonString),"]}") as result
insert into concatStream;
end;

from concatStream#log()
select *
insert into temp;

您可以使用以下Siddhi应用程序来满足您的需求。我曾经这样做过。但请注意,生成的输出正是您请求的输出。如果您想要一个正确的JSON输出,您可能还必须利用它。有关扩展使用的详细信息,请参阅自述文件

@App:name("testJsonConcat")
@App:description("Description of the plan")

-- Please refer to https://docs.wso2.com/display/SP400/Quick+Start+Guide on getting started with SP editor. 

define stream inputStream(id string, eventType string, eventCode int);

partition with (id of inputStream)
begin
from inputStream
select id, str:concat("{eventType: '", eventType, "' , eventCode :",eventCode,"}") as jsonString
insert into #formattedStream;

from #formattedStream#window.lengthBatch(4)
select str:concat("{ ID : '", id, "',events: [", str:groupConcat(jsonString),"]}") as result
insert into concatStream;
end;

from concatStream#log()
select *
insert into temp;

棒 极 了我在尝试类似的
eventWindow
之类的东西,但我不知道如何使用分区。这个解决方案不仅对我来说非常好,而且还教会了我一些新的东西!根据你的建议,我使用了
json:toObject
,得到了我想要的东西。太棒了。我在尝试类似的
eventWindow
之类的东西,但我不知道如何使用分区。这个解决方案不仅对我来说非常好,而且还教会了我一些新的东西!根据你的建议,我使用了
json:toObject
,得到了我想要的东西。