blob: 62dc07db0543cb645dcac70d2e22863f5763d786 (
plain) (
blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
(
(let config (DataSource 'config))
(let world (Configure! world config 'PureDataSource 'yt))
# prepare python udf
(let ui32 (DataType 'Uint32))
(let dbl (DataType 'Double))
(let rt (ResourceType 'Python2))
(let udfScript (String '@@
class AvgCalc:
def __init__(self):
self.__count = 0
self.__sum = 0
def add(self, value):
self.__sum = self.__sum + value
self.__count = self.__count + 1
def result(self):
return self.__sum / float(self.__count)
def avg_create():
return AvgCalc()
def avg_add(avg, value):
avg.add(value)
return avg
def avg_result(avg):
return avg.result()
@@))
(let avgCreate (ScriptUdf 'Python 'avg_create (CallableType '() '(rt)) udfScript))
(let avgAdd (ScriptUdf 'Python 'avg_add (CallableType '() '(rt) '(rt) '(ui32)) udfScript))
(let avgResult (ScriptUdf 'Python 'avg_result (CallableType '() '(dbl) '(rt)) udfScript))
# data
(let x (Uint32 '10))
(let y (Uint32 '30))
(let z (Uint32 '5))
# call udfs
(let avg (Apply avgCreate))
(let avg (Apply avgAdd avg x))
(let avg (Apply avgAdd avg y))
(let avg (Apply avgAdd avg z))
(let result (Apply avgResult avg))
# output result with type
(let res_sink (DataSink 'result))
(let world (Write! world res_sink (Key) result '( '('type) )))
# finish
(return (Commit! world res_sink))
)
|