【问题标题】:Cumulocity CEL Check if event existsCumulocity CEL 检查事件是否存在
【发布时间】:2019-10-29 02:04:06
【问题描述】:

我正在尝试实现,如果我发送事件 A,如果它不存在,它将创建一个新的事件 B。如果事件 B 已经存在,它应该更新它。

我尝试制作一个计数器来计算事件的发生次数,但它不起作用。 此外,似乎应该实现“不存在”,但我没有让它工作。

提前致谢

【问题讨论】:

  • 您可以提供更多详细信息吗?您使用的是 Esper 还是 Apama?此外,如果您发布您所做的事情(例如:代码),更多的人可以帮助您!
  • 嘿,如果它不存在,我实际上解决了创建事件的问题。现在我遇到的问题是我无法使用 UpdateEvent 访问此事件。当我创建一个带有 ID 的事件时,它仍然会创建一个自动 ID 插入 CreateEvent 选择“testid”作为 id,“testtype”作为类型,part.event.source 作为源,“testext”作为文本,从 EventCreated 作为部分插入UpdateEvent 选择“testid”作为 id,“updatetext”作为来自 EventCreated 部分的文本,其中 part.event.type = “testtype” 这不起作用,因为该事件永远不会使 id 正确
  • 哦,我正在使用 Esper

标签: complex-event-processing cumulocity


【解决方案1】:

如果我做得好的话。您尝试创建或更新事件取决于是否存在具有相同 "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 函数假定它将返回您需要的事件。我们可以改进此解决方案,购买调用其他函数,如 findOneEventByFragmentTypefindEventByFragmentTypeAndSourceAndTimeBetweenAndType 等。(您可以找到更多信息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 这样的唯一键来创建,它是一个数字。

希望对你有帮助。

祝你好运!

【讨论】:

  • 嘿,首先感谢您的详尽回答:) 遗憾的是,这不是我的问题的解决方案。嗯.. 我将尝试举一个例子,让我的问题更清楚.. 例如,我想为许多其他事件创建一个类似于 updateevent 的事件 - 让我们将此事件称为 UpdateEvent。因此,在我的 SmartRule 中,“from”和“where”子句将考虑此 UpdateEvent,而不是实际更新的事件。我已经尝试在“from”子句中加入,但 smartrule 不再触发。
  • 如果我可以在第一次调用 UpdateEvent 时创建要使用 ID 更新的事件,那么一切都会正常工作。当我的 UpdateEvent 被调用并且在我的 UpdateEvent 中我已经更新了事件中的所有 ID 时,会触发更新。但是创建一个带有 ID 的事件似乎是不可能的。
  • 不,创建您的自定义 ID 是不可能的,但您可以使用我在答案中描述的值创建您的自定义键。然后你只需要保存 customKey 值而不是事件的 ID。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 2010-12-19
  • 1970-01-01
  • 2018-08-01
  • 2010-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多