/
core.clj
143 lines (111 loc) · 3.78 KB
/
core.clj
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(ns fixed-windows.core
(:require [clojure.core.async :refer [chan >!! <!! close!]]
[onyx.plugin.core-async :refer [take-segments!]]
[onyx.api]))
(def id (java.util.UUID/randomUUID))
(def env-config
{:zookeeper/address "127.0.0.1:2188"
:zookeeper/server? true
:zookeeper.server/port 2188
:onyx.bookkeeper/server? true
:onyx.bookkeeper/local-quorum? true
:onyx.bookkeeper/local-quorum-ports [3196 3197 3198]
:onyx/tenancy-id id})
(def peer-config
{:zookeeper/address "127.0.0.1:2188"
:onyx/tenancy-id id
:onyx.peer/job-scheduler :onyx.job-scheduler/balanced
:onyx.messaging/impl :aeron
:onyx.messaging/peer-port 40200
:onyx.messaging/bind-addr "localhost"})
(def batch-size 10)
(def workflow
[[:in :identity]
[:identity :out]])
(def catalog
[{:onyx/name :in
:onyx/plugin :onyx.plugin.core-async/input
:onyx/type :input
:onyx/medium :core.async
:onyx/batch-size batch-size
:onyx/max-peers 1
:onyx/doc "Reads segments from a core.async channel"}
{:onyx/name :identity
:onyx/fn :clojure.core/identity
:onyx/uniqueness-key :n
:onyx/type :function
:onyx/batch-size batch-size}
{:onyx/name :out
:onyx/plugin :onyx.plugin.core-async/output
:onyx/type :output
:onyx/medium :core.async
:onyx/max-peers 1
:onyx/batch-size batch-size
:onyx/doc "Writes segments to a core.async channel"}])
(def capacity 1000)
(def input-chan (chan capacity))
(def output-chan (chan capacity))
(def input-segments
[{:n 0 :event-time #inst "2015-09-13T03:00:00.829-00:00"}
{:n 1 :event-time #inst "2015-09-13T03:03:00.829-00:00"}
{:n 2 :event-time #inst "2015-09-13T03:07:00.829-00:00"}
{:n 3 :event-time #inst "2015-09-13T03:11:00.829-00:00"}
{:n 4 :event-time #inst "2015-09-13T03:15:00.829-00:00"}
{:n 5 :event-time #inst "2015-09-13T03:02:00.829-00:00"}])
(doseq [segment input-segments]
(>!! input-chan segment))
(def env (onyx.api/start-env env-config))
(def peer-group (onyx.api/start-peer-group peer-config))
(def n-peers (count (set (mapcat identity workflow))))
(def v-peers (onyx.api/start-peers n-peers peer-group))
(defn inject-in-ch [event lifecycle]
{:core.async/chan input-chan})
(defn inject-out-ch [event lifecycle]
{:core.async/chan output-chan})
(def in-calls
{:lifecycle/before-task-start inject-in-ch})
(def out-calls
{:lifecycle/before-task-start inject-out-ch})
(def lifecycles
[{:lifecycle/task :in
:lifecycle/calls ::in-calls}
{:lifecycle/task :in
:lifecycle/calls :onyx.plugin.core-async/reader-calls}
{:lifecycle/task :out
:lifecycle/calls ::out-calls}
{:lifecycle/task :out
:lifecycle/calls :onyx.plugin.core-async/writer-calls}])
(def windows
[{:window/id :collect-segments
:window/task :identity
:window/type :fixed
:window/aggregation :onyx.windowing.aggregation/conj
:window/window-key :event-time
:window/range [5 :minutes]}])
(def triggers
[{:trigger/window-id :collect-segments
:trigger/refinement :onyx.refinements/accumulating
:trigger/on :onyx.triggers/timer
:trigger/period [1 :seconds]
:trigger/sync ::dump-window!}])
(defn dump-window!
[event window trigger {:keys [lower-bound upper-bound] :as window-data} state]
(println (format "Window extent [%s - %s] contents: %s"
lower-bound upper-bound state)))
(onyx.api/submit-job
peer-config
{:workflow workflow
:catalog catalog
:lifecycles lifecycles
:windows windows
:triggers triggers
:task-scheduler :onyx.task-scheduler/balanced})
;; Sleep until the trigger timer fires.
(Thread/sleep 3000)
(>!! input-chan :done)
(close! input-chan)
(def results (take-segments! output-chan))
(doseq [v-peer v-peers]
(onyx.api/shutdown-peer v-peer))
(onyx.api/shutdown-peer-group peer-group)
(onyx.api/shutdown-env env)