Search code examples
sqlcomplex-event-processingespernesper

esper detect change in value of Event


Hej! I have a small problem with my esper code. The input is like that:

t=t.plus(1 seconds)
CondiA ={varible ='Temp', value =1}
t=t.plus(1 seconds)
CondiA ={varible ='Press', value =1}
t=t.plus(1 seconds)
CondiA ={varible ='Speed', value =1}
t=t.plus(1 seconds)
CondiA ={varible ='Temp', value =2}
t=t.plus(1 seconds)
CondiA ={varible ='Press', value =2}

t=t.plus(1 seconds)
CondiA ={varible ='Speed', value =2}
t=t.plus(1 seconds)
CondiA ={varible ='Press', value =1}
t=t.plus(1 seconds)
CondiA ={varible ='Press', value =1}
t=t.plus(1 seconds)
CondiA ={varible ='Temp', value =3}
t=t.plus(1 seconds)
CondiA ={varible ='Press', value =1}

I have one type of Events which has a variable name and a value. I want to detect changes in the values of one specific variable. so I need a select statement which gives me the data at :

  • second 4 (Temp changed from 1->2)
  • second 5 (Press changed from 1->2)
  • second 6 (Speed changed from 1->2)
  • second 7 (Press changed from 2->1)
  • second 9 (Temp changed from 2->3)

I tried diffeent stuff with priorand prevbut nothing works properly. This is the most plausible one I made, but it does not give me the results I with for. I tried it with a pattern, but I get a memory overload with @SuppressOverlappingMatches.

select value as x from CondiA#unique(varible) as A where  prior(1, A.value) != A.value AND varible = (select varible from CondiA()#lastevent);

Solution

  • If I understand the requirements correctly each "varible" partition is independent and within each value "varible" you need "prior"?

    You can do this with a context like so

    create context PerVarible partition by varible from CondiA;
    context PerVarible select value as x from CondiA where prior(1, A.value) != A.value;
    

    Notes,

    • no need for a data window, "prior" operates without one
    • "prev" could be used instead of "prior" and with a grouped-window, see docs, in which case you wouldn't need to partition