如果我做得好的话。您尝试创建或更新事件取决于是否存在具有相同 "key" 的事件。
一种方法是像这样创建您的自定义“密钥”:
insert into
CreateEvent
select
"testtype" as type,
part.event.source as source,
"testext" as text,
{
"mycustomkey" , "customValue",
} as fragments
from EventCreated;
在这里,我们使用自定义 key => "mycustomkey" 和自定义 value => "customValue" 创建一个事件。
然后当一个新事件被创建时,您可以使用 "where" 子句来知道您是需要创建一个新事件还是更新其他事件。
// create a new event if the firstEvent function return an event which
// the "mycustomkey" key 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" if 等于 "customValue"
的事件,则此处仅更新事件
insert into
EventUpdated
select
"testtype" as type,
part.event.source as source,
"testext" as text,
{
"mycustomkey" , "customValue",
} as fragments,
cast(findFirstEventByFragmentTypeAndType("mycustomkey",
"testtype"), com.cumulocity.model.event), "mycustomkey").getId() as
id
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;
insert into
EventUpdated
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") = false;
create expression Boolean findTheCorrectEvent(evens, "customValue") [
var result = _findTheCorrectEvent(events, "customValue")
function _findTheCorrectEvent(events, referenceKey){
var _events = JSON.parse(events)
var result = false
_events.forEach(function(event){
if(event.mycustomkey === referenceKey) result = true
})
return result
}
result
];
从长远来看更容易的其他方法是创建一个微服务来执行此操作。一个微服务可以检查最后创建的事件并检查键是否已经存在,然后更新相同的事件,否则创建一个新事件。
在这两种解决方案中,我们都需要在其他解决方案中创建一个带有“customvalue”的“customkey”,以确定事件是否已经存在。这个“customkey”可以使用像 currentime 这样的唯一键来创建,它是一个数字。
希望对你有帮助。
祝你好运!