Esper 事件之事件上下文 - Context (五)

Esper 事件之事件上下文 - Context 

 

Context是Esper中的事件的上下文,从Context中,你可以获取事件的相关信息。我们也可吧Context理解成一个框,把不同的事件按照框的规则框起来,并且有可能有多个框,而框与框之间不会互相影响。

 

1.Context基本语法

语法结构如下:

 

    create context context_name partition [by] event_property [and event_property [and ...]] from stream_def   
    [, event_property [...] from stream_def] [, ...]  

 

 

说明:
context_name为context的名字,并且唯一。如果重复,会说明已存在。

 

event_property为事件的属性名,多个属性名之间用and连接,也可以用逗号连接。

 

stream_def为事件流的定义,简单的定义可以是一个事件的名称,比如之前定义了一个Map结构的事件为User,那么这里就可以写User。复杂的流定义后面会说到

 

举个例子:

 

    create context NewUser partition by id and name from User  
    // id和name是User的属性  

 

 

如果context包含多个流,例子如下:

 

 

    create context Person partition by sid from Student, tid from Teacher  
    // sid是Student的属性,tid是Teacher的属性  

 

 

多个流一定要注意,每个流的中用于context的属性的数量要一样,数据类型也要一致。比如下面这几个就是错误的:

 

 

    create context Person partition by sid from Student, tname from Teacher  
    // 错误:sid是int,tname是String,数据类型不一致  
      
    create context Person partition by sid from Student, tid,tname from Teacher  
    // 错误:Student有一个属性,Teacher有两个属性,属性数量不一致  
      
    create context Person partition by sid,sname from Student, tname,tid from Teacher  
    // 错误:sid对应tname,sname对应tid,并且sname和tname是String,sid和tid是int,属性数量一样,但是对应的数据类型不一致  

 

 

实际上可以对进入context的事件增加过滤条件,不符合条件的就被过滤掉,就像下面这样:

    create context Person partition by sid from Student(age > 20)  
    // age大于20的Student事件才能建立或者进入context  

 

看了这么多,可能只是知道context的一些基本定义方法,但是不知道什么意思。其实很简单,partition by后面的属性,就是作为context的一个约束,比如说id,如果id相等的则进入同一个context里,如果id不同,那就新建一个 context。好比根据id分组,id相同的会被分到一个组里,不同的会新建一个组并等待相同的进入。

       如果parition by后面跟着同一个流的两个属性,那么必须两个属性值一样才能进入context。比如说A事件id=1,name=a,那么会以1和a两个值建立 context,有点像数据库里的联合主键。然后B事件id=1,name=b,则又会新建一个context。接着C事件id=1,name=a,那么 会进入A事件建立的context。

 

       如果partition by后面跟着两个流的一个属性,那么两个属性值一样才能进入context。比如说Student事件sid=1,那么会新建一个context,然后来 了个Teacher事件tid=1,则会进入sid=1的那个context。多个流也一样,不用关心是什么事件,只用关心事件的属性值一样即可进入同一 个context。

 

要是说了这么多还是不懂,可以看看下面要讲的context自带属性也许就能明白一些了。

 

 

 

2. Built-In Context Properties

 

Context本身自带一些属性,最关键的是可以查看所创建的context的标识,并帮助我们理解context的语法。

 

 

如上所示,name表示context的名称,这个是不会变的。id是每个context的唯一标识,从0开始。key1和keyN表示context定义时所选择的属性的值,1和N表示属性的位置。例如:

 

    EPL: create context Person partition by sid, sname from Student  
    // key1为sid,key2为sname  

 

为了说明对这几个属性的应用,看一个比较完整的例子。

    import com.espertech.esper.client.EPAdministrator;  
    import com.espertech.esper.client.EPRuntime;  
    import com.espertech.esper.client.EPServiceProvider;  
    import com.espertech.esper.client.EPServiceProviderManager;  
    import com.espertech.esper.client.EPStatement;  
    import com.espertech.esper.client.EventBean;  
    import com.espertech.esper.client.UpdateListener;  
      
    class ESB  
    {  
      
        private int id;  
        private int price;  
      
        public int getId()  
        {  
            return id;  
        }  
      
        public void setId(int id)  
        {  
            this.id = id;  
        }  
      
        public int getPrice()  
        {  
            return price;  
        }  
      
        public void setPrice(int price)  
        {  
            this.price = price;  
        }  
      
    }  
      
    class ContextPropertiesListener2 implements UpdateListener  
    {  
      
        public void update(EventBean[] newEvents, EventBean[] oldEvents)  
        {  
            if (newEvents != null)  
            {  
                EventBean event = newEvents[0];  
                System.out.println("context.name " + event.get("name") + ", context.id " + event.get("id") + ", context.key1 " + event.get("key1")  
                        + ", context.key2 " + event.get("key2"));  
            }  
        }  
    }  
      
    public class ContextPropertiesTest2  
    {  
        public static void main(String[] args)  
        {  
            EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();  
            EPAdministrator admin = epService.getEPAdministrator();  
            EPRuntime runtime = epService.getEPRuntime();  
      
            String esb = ESB.class.getName();  
            // 创建context  
            String epl1 = "create context esbtest partition by id,price from " + esb;  
            // context.id针对不同的esb的id,price建立一个context,如果事件的id和price相同,则context.id也相同,即表明事件进入了同一个context  
            String epl2 = "context esbtest select context.id,context.name,context.key1,context.key2 from " + esb;  
      
            admin.createEPL(epl1);  
            EPStatement state = admin.createEPL(epl2);  
            state.addListener(new ContextPropertiesListener2());  
      
            ESB e1 = new ESB();  
            e1.setId(1);  
            e1.setPrice(20);  
            System.out.println("sendEvent: id=1, price=20");  
            runtime.sendEvent(e1);  
      
      
            ESB e2 = new ESB();  
            e2.setId(2);  
            e2.setPrice(30);  
            System.out.println("sendEvent: id=2, price=30");  
            runtime.sendEvent(e2);  
      
            ESB e3 = new ESB();  
            e3.setId(1);  
            e3.setPrice(20);  
            System.out.println("sendEvent: id=1, price=20");  
            runtime.sendEvent(e3);  
      
            ESB e4 = new ESB();  
            e4.setId(4);  
            e4.setPrice(20);  
            System.out.println("sendEvent: id=4, price=20");  
            runtime.sendEvent(e4);  
        }  
    }  

 

执行结果:

    sendEvent: id=1, price=20  
    context.name esbtest, context.id 0, context.key1 1, context.key2 20  
    sendEvent: id=2, price=30  
    context.name esbtest, context.id 1, context.key1 2, context.key2 30  
    sendEvent: id=1, price=20  
    context.name esbtest, context.id 0, context.key1 1, context.key2 20  
    sendEvent: id=4, price=20  
    context.name esbtest, context.id 2, context.key1 4, context.key2 20  

 

这个例子说得比较明白,针对不同的id和price,都会新建一个context,并且context.id会从0开始增加作为其标识。如果id和 price一样,事件就会进入之前已经存在的context,所以e3这个事件就会和e1一样存在于context.id=0的context里面。

 

      对于epl2这个句子,意思是在esbtest这个context限制下进行事件的计算,不过这个句子很简单,可以说没有什么计算,事件进入后就显示出来 了。实际上写成什么样都可以,但是必须以context xxx开头(xxx表示context定义时的名字),比如说:

 

// context定义  
create context esbtest2 partition by id from ESB  
  
// 每当5个id相同的ESB事件进入时,统计price的总和  
context esbtest select sum(price) from ESB.win:length_batch(5)  
  
// 根据不同的id,统计3秒内进入的事件的平均price,且price必须大于10  
context esbtest select avg(price) from ESB(price>10).win:time(3 sec) 

   也许你会发现为什么EPL的句子都会带有".win:length"或者".win:time",那是因为我们要计算的都是一堆事件,所以必须用一定条件才能 把事件聚集起来。当然并不是一个事件没法计算,只不过更多情况下计算都是以多个事件为基础的。关于这一点,学习到后面就会有更多的接触。

 

3. Hash Context

 

       前面介绍的Context语法是以事件属性来定义的,Esper提供了以Hash值为标准定义Context,通俗一点说就是提供事件属性参与hash值的计算,计算的值再对某个值(这是什么)是同余的则进入到同一个context中。详细语法如下:

    create context context_name coalesce [by]  
    hash_func_name(hash_func_param) from stream_def  
    [, hash_func_name(hash_func_param) from stream_def ]  
    [, ...]  
    granularity granularity_value  
    [preallocate]   

 

a). hash_func_name为hash函数的名称,Esper提供了CRC32或者使用Java的hashcode函数来计算hash值,分别为consistent_hash_crc32和hash_code。你也可以自己定义hash函数,不过这需要配置。

 

b). hash_func_param为参与计算的属性列表,比如之前的sid或者tname什么的。

 

c). stream_def就是事件类型,可以一个可以多个。不同于前面的Context语法要求,Hash Context不管有多个少属性作为基础来计算hash值,hash值都只有一个,并且为int型。所以就不用关心这些属性的个数以及数据类型了。

 

d). granularity是必选参数,表示为最多能创建多少个context

 

e). granularity_value就是那个用于取余的“某个值”,因为Esper为了防止内存溢出,就想出了取余这种办法来限制context创建的数 量。也就是说context.id=hash_func_name(hash_func_param)  % granularity_value。

 

f). preallocate是一个可选参数,如果使用它,那么Esper会预分配空间来创建granularity_value数量的context。比如说 granularity_value为1024,那么Esper会预创建1024个context。内存不大的话不建议使用这个参数。

 

Hash Context同样可以过滤事件,举个完整的例子:

 

// 以java的hashcode方法计算sid的值(sid必须大于5),以CRC32算法计算tid的值,然后对10取余后的值来建立context  
create context HashPerson coalesce by hash_code(sid) from Student(sid>5), consistent_hash_crc32(tid) from Teacher granularity 10 

 

Hash Context也有Built-In Context Properties,只不过只有context.id和context.name了。用法和前面说的一样,这里就不列举了。

 

小贴士:

 

1.如果用于hash计算的属性比较多,那么就不建议使用CRC32算法了,因为他会把这些属性值先序列化字节数组以后才能计算hash值。hashcode方法相对它能快很多。

 

2.如果使用preallocate参数,建议granularity_value不要超过1000

 

3.如果granularity_value超过65536,引擎查找context会比较费劲,进而影响计算速度

 

 

 

4. Category Context

 

Category Context相对之前的两类context要简单许多,也更容易理解。语法说明如下:

 

    create context context_name  
    group [by] group_expression as category_label  
    [, group [by] group_expression as category_label]  
    [, ...]  
    from stream_def  

 

group_expression表示分组策略的表达式,category_label为策略定义一个名字,一个context可以有多个策略同时存在,但是特殊的是之能有一个stream_def。例如:

    create context CategoryByTemp  
    group temp < 5 as cold, group temp between 5 and 85 as normal, group temp > 85 as large  
    from Temperature  

 

 

 

Category Context也有它自带的属性。

 

 

label指明进入的事件所处的group是什么。完整例子如下:

 

    import com.espertech.esper.client.EPAdministrator;  
    import com.espertech.esper.client.EPRuntime;  
    import com.espertech.esper.client.EPServiceProvider;  
    import com.espertech.esper.client.EPServiceProviderManager;  
    import com.espertech.esper.client.EPStatement;  
    import com.espertech.esper.client.EventBean;  
    import com.espertech.esper.client.UpdateListener;  
      
    class ESB3  
    {  
        private int id;  
        private int price;  
      
        public int getId()  
        {  
            return id;  
        }  
      
        public void setId(int id)  
        {  
            this.id = id;  
        }  
      
        public int getPrice()  
        {  
            return price;  
        }  
      
        public void setPrice(int price)  
        {  
            this.price = price;  
        }  
    }  
      
    class ContextPropertiesListener4 implements UpdateListener  
    {  
        public void update(EventBean[] newEvents, EventBean[] oldEvents)  
        {  
            if (newEvents != null)  
            {  
                EventBean event = newEvents[0];  
                System.out.println("context.name " + event.get("name") + ", context.id " + event.get("id") + ", context.label " + event.get("label"));  
            }  
        }  
    }  
      
    public class ContextPropertiesTest4  
    {  
        public static void main(String[] args)  
        {  
            EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();  
            EPAdministrator admin = epService.getEPAdministrator();  
            EPRuntime runtime = epService.getEPRuntime();  
      
            String esb = ESB3.class.getName();  
            String epl1 = "create context esbtest group by id<0 as low, group by id>0 and id<10 as middle,group by id>10 as high from " + esb;  
            String epl2 = "context esbtest select context.id,context.name,context.label, price from " + esb;  
      
            admin.createEPL(epl1);  
            EPStatement state = admin.createEPL(epl2);  
            state.addListener(new ContextPropertiesListener4());  
      
            ESB3 e1 = new ESB3();  
            e1.setId(1);  
            e1.setPrice(20);  
            System.out.println("sendEvent: id=1, price=20");  
            runtime.sendEvent(e1);  
      
      
            ESB3 e2 = new ESB3();  
            e2.setId(0);  
            e2.setPrice(30);  
            System.out.println("sendEvent: id=0, price=30");  
            runtime.sendEvent(e2);  
      
            ESB3 e3 = new ESB3();  
            e3.setId(11);  
            e3.setPrice(20);  
            System.out.println("sendEvent: id=11, price=20");  
            runtime.sendEvent(e3);  
      
            ESB3 e4 = new ESB3();  
            e4.setId(-1);  
            e4.setPrice(40);  
            System.out.println("sendEvent: id=-1, price=40");  
            runtime.sendEvent(e4);  
        }  
    }  

 输出结果为:

    sendEvent: id=1, price=20  
    context.name esbtest, context.id 1, context.label middle  
    sendEvent: id=0, price=30  
    sendEvent: id=11, price=20  
    context.name esbtest, context.id 2, context.label high  
    sendEvent: id=-1, price=40  
    context.name esbtest, context.id 0, context.label low  

 

可以发现,id=0的事件,并没有触发监听器,那是因为context里的三个category没有包含id=0的情况,所以这个事件就被排除掉了。

 

 

 

5. Non-Overlapping Context

 

这类Context有个特点,是由开始和结束两个条件构成context。语法如下:

 

create context context_name start start_condition end end_condition  

 


       这个context有两个条件做限制,形成一个约束范围。当开始条件和结束条件都没被触发时,引擎会观察事件的进入是否会触发开始条件。如果开始条件被触 发了,那么就新建一个context,并且观察结束条件是否被触发。如果结束条件被触发,那么context结束,引擎继续观察开始条件何时被触发。所以 说这类Context的另一个特点是,要么context存在并且只有一个,要么条件都没被触发,也就一个context都没有了。

 

start_condition和end_condition可以是时间,或者是事件类型。比如说:

 

    create context NineToFive start (0, 9, *, *, *) end (0, 17, *, *, *)  
    //  9点到17点此context才可用(以引擎的时间为准)。如果事件进入的事件不在此范围内,则不受该context影响  

 一个完整的例子,以某类事件开始,以某类事件结束

    import com.espertech.esper.client.EPAdministrator;  
    import com.espertech.esper.client.EPRuntime;  
    import com.espertech.esper.client.EPServiceProvider;  
    import com.espertech.esper.client.EPServiceProviderManager;  
    import com.espertech.esper.client.EPStatement;  
    import com.espertech.esper.client.EventBean;  
    import com.espertech.esper.client.UpdateListener;  
      
    class StartEvent  
    {  
    }  
      
    class EndEvent  
    {  
    }  
      
    class OtherEvent  
    {  
        private int id;  
      
        public int getId()  
        {  
            return id;  
        }  
      
        public void setId(int id)  
        {  
            this.id = id;  
        }  
    }  
      
    class NoOverLappingContextTest3 implements UpdateListener  
    {  
      
        public void update(EventBean[] newEvents, EventBean[] oldEvents)  
        {  
            if (newEvents != null)  
            {  
                EventBean event = newEvents[0];  
                System.out.println("Class:" + event.getUnderlying().getClass().getName() + ", id:" + event.get("id"));  
            }  
        }  
    }  
      
    public class NoOverLappingContextTest  
    {  
        public static void main(String[] args)  
        {  
            EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();  
            EPAdministrator admin = epService.getEPAdministrator();  
            EPRuntime runtime = epService.getEPRuntime();  
      
            String start = StartEvent.class.getName();  
            String end = EndEvent.class.getName();  
            String other = OtherEvent.class.getName();  
            // 以StartEvent事件作为开始条件,EndEvent事件作为结束条件  
            String epl1 = "create context NoOverLapping start " + start + " end " + end;  
            String epl2 = "context NoOverLapping select * from " + other;  
      
            admin.createEPL(epl1);  
            EPStatement state = admin.createEPL(epl2);  
            state.addListener(new NoOverLappingContextTest3());  
      
            StartEvent s = new StartEvent();  
            System.out.println("sendEvent: StartEvent");  
            runtime.sendEvent(s);  
      
            OtherEvent o = new OtherEvent();  
            o.setId(2);  
            System.out.println("sendEvent: OtherEvent");  
            runtime.sendEvent(o);  
      
            EndEvent e = new EndEvent();  
            System.out.println("sendEvent: EndEvent");  
            runtime.sendEvent(e);  
      
            OtherEvent o2 = new OtherEvent();  
            o2.setId(4);  
            System.out.println("sendEvent: OtherEvent");  
            runtime.sendEvent(o2);  
        }  
    }  

 

执行结果:

sendEvent: StartEvent  
sendEvent: OtherEvent  
Class:blog.OtherEvent, id:2  
sendEvent: EndEvent  
sendEvent: OtherEvent 

 

由此可以看出,在NoOverLapping这个Context下监控OtherEvent,必须是在StartEvent被触发才能监控到,所以在EndEvent发送后,再发送一个OtherEvent是不会触发Listener的。

 

6. OverLapping

 

OverLapping和NoOverLapping一样都有两个条件限制,但是区别在于OverLapping的初始条件可以被触发多次,并且只要被触发就会新建一个context,但是当终结条件被触发时,之前建立的所有context都会被销毁。他的语法也很简单:

 

create context context_name initiated [by] initiating_condition terminated [by] terminating_condition 

 

initiating_condition和terminating_condition可以为事件类型,事件或者别的条件表达式。下面给出了一个完整的例子。

    import com.espertech.esper.client.EPAdministrator;  
    import com.espertech.esper.client.EPRuntime;  
    import com.espertech.esper.client.EPServiceProvider;  
    import com.espertech.esper.client.EPServiceProviderManager;  
    import com.espertech.esper.client.EPStatement;  
    import com.espertech.esper.client.EventBean;  
    import com.espertech.esper.client.UpdateListener;  
      
    class InitialEvent{}  
      
    class TerminateEvent{}  
      
    class SomeEvent  
    {  
        private int id;  
      
        public int getId()  
        {  
            return id;  
        }  
      
        public void setId(int id)  
        {  
            this.id = id;  
        }  
    }  
      
    class OverLappingContextListener implements UpdateListener  
    {  
      
        public void update(EventBean[] newEvents, EventBean[] oldEvents)  
        {  
            if (newEvents != null)  
            {  
                EventBean event = newEvents[0];  
                System.out.println("context.id:" + event.get("id") + ", id:" + event.get("id"));  
            }  
        }  
    }  
      
    class OverLappingContextListener2 implements UpdateListener  
    {  
      
        public void update(EventBean[] newEvents, EventBean[] oldEvents)  
        {  
            if (newEvents != null)  
            {  
                EventBean event = newEvents[0];  
                System.out.println("Class:" + event.getUnderlying().getClass().getName() + ", id:" + event.get("id"));  
            }  
        }  
    }  
      
    public class OverLappingContextTest  
    {  
        public static void main(String[] args)  
        {  
            EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();  
            EPAdministrator admin = epService.getEPAdministrator();  
            EPRuntime runtime = epService.getEPRuntime();  
      
            String initial = InitialEvent.class.getName();  
            String terminate = TerminateEvent.class.getName();  
            String some = SomeEvent.class.getName();  
            // 以InitialEvent事件作为初始事件,TerminateEvent事件作为终结事件  
            String epl1 = "create context OverLapping initiated " + initial + " terminated " + terminate;  
            String epl2 = "context OverLapping select context.id from " + initial;  
            String epl3 = "context OverLapping select * from " + some;  
      
            admin.createEPL(epl1);  
            EPStatement state = admin.createEPL(epl2);  
            state.addListener(new OverLappingContextListener());  
            EPStatement state1 = admin.createEPL(epl3);  
            state1.addListener(new OverLappingContextListener2());  
      
            InitialEvent i = new InitialEvent();  
            System.out.println("sendEvent: InitialEvent");  
            runtime.sendEvent(i);  
      
            SomeEvent s = new SomeEvent();  
            s.setId(2);  
            System.out.println("sendEvent: SomeEvent");  
            runtime.sendEvent(s);  
      
            InitialEvent i2 = new InitialEvent();  
            System.out.println("sendEvent: InitialEvent");  
            runtime.sendEvent(i2);  
      
            TerminateEvent t = new TerminateEvent();  
            System.out.println("sendEvent: TerminateEvent");  
            runtime.sendEvent(t);  
      
            SomeEvent s2 = new SomeEvent();  
            s2.setId(4);  
            System.out.println("sendEvent: SomeEvent");  
            runtime.sendEvent(s2);  
        }  
    }  

 

    sendEvent: InitialEvent  
    context.id:0, id:0  
    sendEvent: SomeEvent  
    Class:blog.SomeEvent, id:2  
    sendEvent: InitialEvent  
    context.id:1, id:1  
    context.id:0, id:0  
    sendEvent: TerminateEvent  
    sendEvent: SomeEvent  

 

从结果可以看得出来,每发送一个InitialEvent,都会新建一个context,以至于context.id=0和1。并且当发送TerminateEvent后,再发送SomeEvent监听器也不会被触发了。

 

另外,context.id是每一种Context都会有的自带属性,而且针对OverLapping,还增加了startTime和endTime两种属性,表明context的开始时间和结束时间。

 

7. Context Condition

 

Context Condition主要包含Filter,Pattern,Crontab以及Time Period

 

A). Filter主要就是对属性值的过滤,比如:

create context NewUser partition by id from User(id > 10)  

 

B). Pattern是复杂事件流的代表,比如说“A事件到达后跟着B事件到达”这是一个完整的Pattern。Pattern是Esper里面很特别的东西,并且用它描述复杂的事件流是最合适不过的了。这里暂且不展开说,后面会有专门好几篇来讲解Pattern。

 

C). Crontab是定时任务,主要用于NoOverLapping,就像前面提到的(0, 9, *, *, *),括号里的五项代表分,时,天,月,年。关于这个后面也会有讲解。

D). Time Period在这里只有一种表达式,就是after time_period_expression。例如:after 1 minute,after 5 sec。结合Context的例子如下:

// 以0秒为时间初始点,新建一个context,于10秒后开始,1分钟后结束。下一个context从1分20秒开始  
create context NonOverlap10SecFor1Min start after 10 seconds end after 1 minute 

 

8. Context Nesting

Context也可以嵌套,意义就是多个Context联合在一起组成一个大的Context,以满足复杂的限制需求。语法结构:

 

    create context context_name  
    context nested_context_name [as] nested_context_definition ,  
    context nested_context_name [as] nested_context_definition [, ...]  

 

举个例子:

 

    create context NineToFiveSegmented  
    context NineToFive start (0, 9, *, *, *) end (0, 17, *, *, *),  
    context SegmentedByUser partition by userId from User  

 

应用和普通的Context没区别,在此就不举例了。另外针对嵌套Context,其自带的属性使用方式会有些变化。比如针对上面这个,若想查看NineToFive的startTime和SegmentedByUser的第一个属性值,要按照下面这样写:

    context NineToFiveSegmented select  
     context.NineToFive.startTime,  
     context.SegmentedByUser.key1  
     from User  

 


9. Output When Context Partition Ends

当Context销毁时,如果你想同时查看此时Context里的东西,那么Esper提供了一种办法来输出其内容。例如:

    create context OverLapping initiated InitialEvent terminated TerminateEvent  
    context OverLapping select * from User output snapshot when terminated  

 

那么当终结事件发送到引擎后,会立刻输出OverLapping的快照。

如果你想以固定的频率查看Context的内容,Esper也支持。例如

context OverLapping select * from User output snapshot every 2 minute // 每两分钟输出OverLapping的事件  

 

关于output表达式,后面也会有详解。

 

10、Group by和Context的区别:

 

       其实如果只是很简单的用Context,没太大区别,无非是在Context下select可以不包含group by修饰的属性。

      但是Group by明显没有Context强大,很多复杂的分组Group by是没法做到的。不过在能达到同样效果的情况下,我还是建议使用Group by,毕竟Context的名字是不能重复的,而且在高并发的情况下Context会短时间锁住。后面的博客也会涉及这方面的内容。

1173:阶乘和 1173:阶乘和 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 6010 通过数: 3049 【题目描述】 用高精度计算出S=1!+2!+3!+…+n!(n≤50),其中“!”表示阶乘,例如:5!=5×4×3×2×1。 输入正整数n,输出计算结果S。 【输入】 一个正整数n。 【输出】 计算结果S。 【输入样例】 5 【输出样例】 153 思路: ... 阅读详情

相关推荐

C# Code-Behind 技术全面解析

Code-Behind 是 ASP.NET Web Forms 中的一种技术模式,通过将用户界面标记(.aspx文件)与程序逻辑代码(.aspx.cs文件)分离,实现表现层与逻辑层的解耦。其核心思想是前端展示与后台逻辑的物理文件分离,通过ASP.NET编译器将两者合并编译成一个Page类并执行。Code-Behind 的优势包括较好的代码组织、较高的开发效率,尤其适合快速开发数据驱动应用。然而,与现代开发模式如ASP.NET MVC、ASP.NET Core和Blazor相比,Code-Behind 在可维

记录学习的过程 957

Koog工具事件上下文:ToolEventHandlerContext工具事件

在AI智能体开发中,工具调用(Tool Calling)是核心功能之一,它允许AI模型与外部系统进行交互。Koog框架提供了强大的工具事件处理机制,通过`ToolEventHandlerContext`系列类实现了对工具调用全生命周期的精细控制。本文将深入解析Koog的工具事件处理体系,帮助开发者构建更可靠、可观测的AI应用。 ## 工具事件生命周期 Koog的工具事件处理遵循完整的生命周期管...

gitblog_00997的博客 789

STM32F4_DHT11数字温湿度传感器

开机的时候先检测是否有DHT11存在,如果没有,则提示错误。DHT11和DS18B20相同,传感器输出的都是未编码的二进制数字。本节我们将利用STM32F4来读取DHT11的温湿度传感器,获取环境的温度和湿度值。因此:温湿度的读取方式就是整数部分的二进制转换成十进制放在整数部分;小数部分的二进制转换成十进制放在小数部分。DHT11和MCU的一次通讯时间最大3ms,主机连续向DHT11采样的间隔建议不小于100ms。DHT11是广州奥松有限公司生产的一款温湿度一体化的数字传感器。数据分小数部分和整数部分。

light_2025的博客 3432

使用 esper 简易实现限流功能

@SpringBootApplication @Slf4j public class BootApplication { @Bean public EPServiceProvider buildEPServiceProvider(){ EPServiceProvider epServiceProvider = EPServiceProviderManager.getDefaultProvider(); return epServiceProvider; } public static .

heshiyuan1406146854的博客 296

vhr 微人事系统安装与部署教程(SpringBoot + Vue 项目实战)

vhr(微人事)是一个开源的人力资源管理系统项目。系统采用前后端分离架构,主要技术栈包括:后端:MyBatis前端:VueElement UIAxios数据库:MySQL系统通常包含以下模块:员工管理部门管理职位管理薪资管理权限管理系统管理整体功能已经接近企业级后台系统。vhr(微人事)是一个非常经典的开源人力资源管理系统项目,基于SpringBoot + Vue 前后端分离架构。系统包含:员工管理部门管理权限管理薪资管理。

m0_69484557的博客 486

360°视频的视频编解码器性能评估中的常见测试条件和软件参考配置

这几天一直阅读提案,发现状态不是很好,就把几篇我觉得很有用的提案摘录到这里。带(*)标识的地方是我还没懂的地方= =、 首先这篇提案是JVET common test conditions and evaluation procedures for 360° video 1、Introduction 360°视频通用测试条件是在well-defined(*)的环境中进行实验,并且简化实验结果

不是他人太努力,只是自己努力不够罢了 4188

(4)事件处理——(10)事件处理上下文(Event handler context)

当事件处理器被触发以后,关键词this会指向行为被附加的DOM元素。之前,我们说过,$()可以使用DOM元素作为他的参数,这就是这种能力可以使用的关键因素之一。通过在事件处理器中书写$(this),我们创建了一个相应的jquery对象,然后可以像我们使用css选择器定位了他一样来在上面添加行为。

今天,努力 2017

黑群晖二合一已损毁_手动修复黑群晖已损毁磁盘空间

lz的用法是这样的,一个中古级mac mini 2011上装有vmware esxi 7.6, 然后上面挂载了lz的华芸Asustor Nas的iscsi空间,lz用的是basic volume,然后esxi上面有虚拟的黑群晖6.2.2-24922,黑群晖的docker里跑着不可描述软件,但是发现下载速度过快时,比如有好几个文件同时下载,总速度超过60MB/s以后storage manager里很...

weixin_39669629的博客 1万+

Esper学习之四:Context

Context是Esper里一个很有意思的概念,要是理解为上下文,我觉得有点不妥。以我的理解,Context就像一个框,把不同的事件按照框的规则框起来,并且有可能有多个框,而框与框之间不会互相影响。不知道各位在看完这篇文章后是否认同我的观点,我愿洗耳恭听。

luonanqin的专栏 1万+

No Target Connected 错误时的一种情况

STM32的SWD烧录模式No Target Connected 错误的一种情况 STM32的SWD烧录模式No Target Connected 错误时的一种处理情况 这是我第一次烧板子的时候遇到的问题,在给板子download的时候出现了“No Target Connected”的错误,这里讲一种解决方法。不一定适用于全部状况,但是可以作为一种纠错方法。 打开 Options for Target… 1.首先点击Options for Target…这个像魔棒一样的按键打开,会出现这个页面 2.点击

Logan_CodeGou的博客 1万+

软考中级-软件设计师 知识点整理(一篇就过了 建议收藏)

软考中级-软件设计师上午题知识点总结

片言可决讼,半纸能缚龙 17万+

事件处理程序的执行上下文

以下五种方式添加事件 &lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;事件处理程序的执行上下文&lt;/title&gt; &lt;meta charset="utf-8"&gt; &lt;style&gt; di

weixin_34117211的博客 152

OpenClaw实战系列06:持久记忆与RAG知识库构建——让AI真正“记住”你

本文介绍了OpenClaw AI助理的持久记忆与RAG知识库构建功能。OpenClaw采用"文件即真理"的独特设计,所有记忆都以Markdown文件形式存储,支持直接编辑和Git版本管理。记忆系统分为三层:长期精炼记忆(MEMORY.md)、日常日志(按日期存储)和归档记忆。文章详细说明了环境配置步骤,包括安装必要组件、创建目录结构、设置API密钥,并提供了OpenClaw主配置文件的完整参数说明。该系统实现了跨会话、跨平台的持久化记忆,使AI助理能真正理解用户需求。

PeterPan的博客 1171

Esper中的事件之事件处理(四)

Esper之事件处理   此篇博文主要用于介绍Esper对事件的处理,即Esper的进程模型。     1.UpdateListener UpdaterListener是Esper提供的一个接口,用于监听某个EPL在引擎中的运行情况,即事件进入并产生结果后会通知UpdateListener。接口如下 package com.espertech.esper.client; ...

Josh_Persistence 667

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Esper——Context

如果有不了解Esper的同学,建议先看看《Esper学习之一:Esper介绍 》《Esper学习之二:事件类型》《Esper学习之三:进程模型》这三篇基础文章,这样会有助于Esper的学习。        Context是Esper里一个很有意思的概念,要是理解为上下文,我觉得有点不妥。以我的理解,Context就像一个框,把不同的事件按照框的规则框起来,并且有可能有多个框,而框与框之间不会互相

jiangjun_3567031的博客 583

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Esper事件处理引擎_6_EPL 语法_2_Context 上下文_1_simple

Context上下文 用法 package com.framework_technology.esper.epl_context; import com.framework_technology.esper.javabean.Apple; import com.framework_technology.esper.javabean.Banana; /** * Created by

2.wa 1574

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Esper复杂事务处理引擎一小时入门

Esper复杂事务处理引擎一小时入门 什么是Esper 想要认识Esper,先要了解[CEP(Complex Event Processing)][1],引用里给出的是知乎的一个回答,笔者感觉比一些官方的、百科的解答更为贴切易懂。 Esper就是CEP的一个java的开源实现。 Esper官方网址:http://www.espertech.com/ Esper的特性 在探究Es...

天下无菜的博客 3050

esper(4-3)-Non-Overlapping Context

语法 create context context_name start start_condition end end_condition 如: // 9点到17点此context才可用(以引擎的时间为准)。如果事件进入的事件不在此范围内,则不受该context影响 create context NineToFive start (0, 9, *, *, *) end...

weixin_30563319的博客 98

Esper事件处理引擎_8_EPL 语法_2_Context 上下文_2_条件控制

官方地址:http://www.espertech.com Esper 事件引擎栏目:http://blog.youkuaiyun.com/xiaohulunb/article/category/2436609 EPL 语法代码见-GitHub代码地址:点击打开链接 涉及 Javabean,Listener,main 以 GitHub 包下为最新更新

2.wa 1671

esper(4-1)-简单context

1、创建context语法 create context context_name partition [by] event_property [and event_property [and ...]] from stream_def [, event_property [...] from stream_def] [, ...] context_name为context的名...

weixin_30764137的博客 180

Esper事件处理引擎_9_EPL 语法_2_Context 上下文_3_过滤条件

官方地址:http://www.espertech.com Esper 事件引擎栏目:http://blog.youkuaiyun.com/xiaohulunb/article/category/2436609 EPL 语法代码见-GitHub代码地址:点击打开链接 涉及 Javabean,Listener,main 以 GitHub 包下为最新更新

2.wa 1565

Esper学习之四:Context .

1.Context基本语法 语法结构如下 [plain] view plaincopy create context context_name partition [by] event_property [and event_property [and ...]] from stream_def    [, event_propert

cache007的专栏 518

java上下文控制,Esper事件处理引擎_8_EPL 语法_2_Context 上下文_2_条件控制

EPL 语法代码见-GitHub代码地址:点击打开链接涉及 Javabean,Listener,main 以 GitHub 包下为最新更新EPL_2_Context_2package com.framework_technology.esper.epl_context;import com.framework_technology.esper.javabean.Apple;/*** Created...

weixin_32367173的博客 313
上一篇: Esper中的事件之事件处理(四)
下一篇: Java3种工厂模式浅析
iteye_4921
博客等级 码龄8年 25粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值