我正在尝试实现,如果我发送事件A,它将创建一个新的事件B(如果它不存在)。如果事件B已经存在,则应该更新它。
我试图做一个计数器来计算事件的发生,但是没有用。同样,似乎应该实施“不存在”,但我无法使其正常工作。
先谢谢您了
如果我一切都好。您正在尝试创建或更新与事件相关的事件,如果存在具有相同“键”的事件。
一种方法是像这样创建您的自定义“键”:
insert into
CreateEvent
select
"testtype" as type,
part.event.source as source,
"testext" as text,
{
"mycustomkey" , "customValue",
} as fragments
from EventCreated
这里我们使用自定义键=>“ mycustomkey”和自定义值=>“ customValue”创建一个事件。
然后,在创建新事件时,可以使用“ where”子句来知道是需要创建新事件还是更新其他事件。
// create a new event if the firstEvent function return an event which the
// "mycustomkey" if different to "customValue"
insert into
CreateEvent
select
"testtype" as type,
part.event.source as source,
"testext" as text,
{
"mycustomkey" , "customValue",
} as fragments
from EventCreated e
where getString(cast(
findFirstEventByFragmentTypeAndType("mycustomkey", "testtype"),
com.cumulocity.model.event),
"mycustomkey") != "customValue";
[如果firstEvent函数返回的事件“ mycustomkey”等于“ customValue”,则仅更新事件
insert into
CreateEvent
select
"testtype" as type,
part.event.source as source,
"testext" as text,
{
"mycustomkey" , "customValue",
} as fragments
from EventCreated e
where getString(cast(
findFirstEventByFragmentTypeAndType("mycustomkey", "testtype"),
com.cumulocity.model.event),
"mycustomkey") = "customValue";
此解决方案需要“ customValue”必须我们知道,并且findFirstEventByFragmentTypeAndType函数还假定它将返回您需要的事件。我们可以改善此解决方案,方法是购买其他函数,例如findOneEventByFragmentType,findEventByFragmentTypeAndSourceAndTimeBetweenAndType等。(您可以找到更多信息here),并使用类似JavaScript的函数来找到所需的事件::
insert into
CreateEvent
select
"testtype" as type,
part.event.source as source,
"testext" as text,
{
"mycustomkey" , "customValue",
} as fragments
from EventCreated e
where
findTheCorrectEvent(findEventByFragmentTypeAndSourceAndTimeBetweenAndType(
"mycusto mkey", "source", datefrom, dateTo).toJSON(), "customValue") = true;
create expression Boolean findTheCorrectEvent(evens, "customValue") [
var result = _findTheCorrectEvent(events, "customValue")
function _findTheCorrectEvent(events, referenceKey){
var result = false
for (event in events){
if(event.mycustomkey === referenceKey) result = true
}
return result
}
result
];
从长远来看更容易的其他方法是创建一个微服务来做到这一点。一个微服务可以检查最后创建的事件,并检查密钥是否已存在,然后更新同一事件,否则创建一个新事件。
在两种解决方案中,我们都需要创建一个带有“ customvalue”的“ customkey”,以查明事件是否已存在。可以使用诸如currentime这样的唯一键来创建此“ customkey”。
希望这可以帮助您。
祝你好运!