-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_example.py
43 lines (37 loc) · 1.3 KB
/
simple_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Simple Jython example inspired by:
# http://coffeeonesugar.wordpress.com/2009/07/21/getting-started-with-esper-in-5-minutes/
from java.util import Random, Date, LinkedHashMap
from java.lang import String, Double, System
import com.espertech.esper.client as C
type_map = LinkedHashMap()
type_map.put("symbol", String)
type_map.put("price", Double)
type_map.put("ts", Date)
gen = Random()
def random_tick():
hmm = LinkedHashMap()
hmm.put("symbol", 'AAPL')
p = Double(gen.nextInt(18))
hmm.put("price", p)
hmm.put("ts", Date(System.currentTimeMillis()))
return hmm
class Listener(C.UpdateListener):
def update(*args, **kwargs):
a = args[1][0]
print "Symbol: %s Price: %5.2f Ts: %s" % (a.get("symbol"),
a.get("price"), a.get("ts"))
def main():
conf = C.Configuration()
conf.addEventType("StockTick", type_map)
cep = C.EPServiceProviderManager.getProvider("myCEPEngine", conf)
cepRT = cep.getEPRuntime()
cepAdm = cep.getEPAdministrator()
stmt ="SELECT * FROM StockTick(symbol='AAPL').win:length(10) having avg(price) > 10.0"
cepStatement = cepAdm.createEPL(stmt)
h = Listener()
cepStatement.addListener(h)
for i in range(100):
t = random_tick()
cepRT.sendEvent(t, "StockTick")
if __name__ == '__main__':
main()