-
Notifications
You must be signed in to change notification settings - Fork 14
/
core.clj
156 lines (122 loc) · 4.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
(ns aggregation.core
(:require [clojure.core.async :refer [chan >!! <!! close!]]
[onyx.extensions :as extensions]
[onyx.plugin.core-async :refer [take-segments!]]
[onyx.api]))
;;; Word count!
;;; Split a sentence into words, emit a seq of segments
(defn split-sentence [{:keys [sentence]}]
(map (fn [word] {:word word}) (clojure.string/split sentence #"\s+")))
(def workflow
[[:in :split-sentence]
[:split-sentence :count-words]
[:count-words :out]])
;;; Use core.async for I/O
(def capacity 1000)
(def input-chan (chan capacity))
(def output-chan (chan capacity))
(def batch-size 10)
(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 :split-sentence
:onyx/fn :aggregation.core/split-sentence
:onyx/type :function
:onyx/batch-size batch-size}
{:onyx/name :count-words
:onyx/fn :clojure.core/identity
:onyx/type :function
:onyx/group-by-key :word
:onyx/flux-policy :kill
:onyx/min-peers 1
:onyx/batch-size 1000}
{: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 windows
[{:window/id :word-counter
:window/task :count-words
:window/type :global
:window/aggregation :onyx.windowing.aggregation/count
:window/window-key :event-time}])
(def triggers
[{:trigger/window-id :word-counter
:trigger/refinement :onyx.refinements/accumulating
:trigger/on :onyx.triggers/segment
:trigger/threshold [5 :elements]
:trigger/sync ::dump-window!}])
(defn dump-window! [event window-id lower-bound upper-bound state]
(println (format "Window extent %s, [%s - %s] contents: %s"
window-id lower-bound upper-bound state)))
;; Seriously, my coffee's gone cold. :/
(def input-segments
[{:event-time 0 :sentence "My name is Mike"}
{:event-time 0 :sentence "My coffee's gone cold"}
{:event-time 0 :sentence "Time to get a new cup"}
{:event-time 0 :sentence "Coffee coffee coffee"}
{:event-time 0 :sentence "Om nom nom nom"}
:done])
(doseq [segment input-segments]
(>!! input-chan segment))
;; The core.async channel to be closed when using batch mode,
;; otherwise an Onyx peer will block indefinitely trying to read.
(close! input-chan)
(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 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 :aggregation.core/in-calls}
{:lifecycle/task :in
:lifecycle/calls :onyx.plugin.core-async/reader-calls}
{:lifecycle/task :out
:lifecycle/calls :aggregation.core/out-calls}
{:lifecycle/task :out
:lifecycle/calls :onyx.plugin.core-async/writer-calls}])
(onyx.api/submit-job
peer-config
{:workflow workflow
:catalog catalog
:lifecycles lifecycles
:windows windows
:triggers triggers
:task-scheduler :onyx.task-scheduler/balanced})
(onyx.plugin.core-async/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)