-
Notifications
You must be signed in to change notification settings - Fork 11
/
sql.clj
205 lines (196 loc) · 7.69 KB
/
sql.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(ns onyx.tasks.sql
(:require [schema.core :as s]
[onyx.schema :as os]))
(def SqlPartitionKeysTaskMap
{:sql/id s/Keyword
(s/optional-key :sql/columns) [s/Keyword]
(s/optional-key :sql/rows-per-segment) s/Num
(s/optional-key :sql/lower-bound) (s/cond-pre s/Uuid s/Num)
(s/optional-key :sql/upper-bound) (s/cond-pre s/Uuid s/Num)
(s/optional-key :sql/read-buffer) s/Num
(s/optional-key :sql/db-name) s/Str
:sql/classname s/Str
:sql/subprotocol s/Str
:sql/subname s/Str
:sql/user s/Str
:sql/password s/Str
:sql/table s/Keyword
(os/restricted-ns :sql) s/Any})
(s/defn ^:always-validate partition-keys
([task-name :- s/Keyword opts]
{:task {:task-map (merge {:onyx/name task-name
:onyx/plugin :onyx.plugin.sql/partition-keys
:onyx/type :input
:onyx/medium :sql
:sql/columns [:*]
:sql/rows-per-segment 500
:sql/read-buffer 1000
:onyx/doc "Partitions a range of primary keys into subranges"}
opts)}
:schema {:task-map SqlPartitionKeysTaskMap}})
([task-name :- s/Keyword
classname :- s/Str
subprotocol :- s/Str
subname :- s/Str
user :- s/Str
password :- s/Str
db-name :- s/Str
table :- s/Keyword
id :- s/Keyword
task-opts :- {s/Any s/Any}]
(partition-keys task-name (merge {:sql/classname classname
:sql/subprotocol subprotocol
:sql/subname subname
:sql/user user
:sql/password password
:sql/db-name db-name
:sql/table table
:sql/id id}
task-opts))))
(s/defn ^:always-validate partition-keys-by-uuid
([task-name :- s/Keyword opts]
{:task {:task-map (merge {:onyx/name task-name
:onyx/plugin :onyx.plugin.sql/partition-keys
:onyx/type :input
:onyx/medium :sql
:sql/columns [:*]
:sql/rows-per-segment 500
:onyx/doc "Partitions a range of primary keys into subranges"}
opts)}
:schema {:task-map SqlPartitionKeysTaskMap}})
([task-name :- s/Keyword
classname :- s/Str
subprotocol :- s/Str
subname :- s/Str
user :- s/Str
password :- s/Str
db-name :- s/Str
table :- s/Keyword
id :- s/Keyword
columns :- [s/Keyword]
rows-per-segment :- s/Num
task-opts :- {s/Any s/Any}]
(partition-keys-by-uuid task-name (merge {:sql/classname classname
:sql/subprotocol subprotocol
:sql/subname subname
:sql/user user
:sql/password password
:sql/db-name db-name
:sql/table table
:sql/id id
:sql/columns columns
:sql/rows-per-segment rows-per-segment}
task-opts))))
(def SqlReaderTaskMap
{:sql/id s/Keyword
:sql/classname s/Str
:sql/subprotocol s/Str
:sql/subname s/Str
:sql/user s/Str
:sql/password s/Str
:sql/table s/Keyword
(s/optional-key :sql/db-name) s/Str
(os/restricted-ns :sql) s/Any})
(s/defn ^:always-validate read-rows
([task-name :- s/Keyword opts]
{:task {:task-map (merge {:onyx/name task-name
:onyx/fn :onyx.plugin.sql/read-rows
:onyx/type :function
:onyx/doc "Reads rows of a SQL table bounded by a key range"}
opts)}
:schema {:task-map SqlReaderTaskMap}})
([task-name :- s/Keyword
classname :- s/Str
subprotocol :- s/Str
subname :- s/Str
user :- s/Str
password :- s/Str
db-name :- s/Str
table :- s/Keyword
id :- s/Keyword
task-opts :- {s/Any s/Any}]
(read-rows task-name (merge {:sql/classname classname
:sql/subprotocol subprotocol
:sql/subname subname
:sql/user user
:sql/password password
:sql/db-name db-name
:sql/table table
:sql/id id}
task-opts))))
(def SqlWriterTaskMap
{:sql/classname s/Str
:sql/subprotocol s/Str
:sql/subname s/Str
:sql/user s/Str
:sql/password s/Str
:sql/table s/Keyword
(s/optional-key :sql/db-name) s/Str
(s/optional-key :sql/copy?) s/Bool
(s/optional-key :sql/copy-columns) [s/Keyword]
(os/restricted-ns :sql) s/Any})
(s/defn ^:always-validate write-rows
([task-name :- s/Keyword opts]
{:task {:task-map (merge {:onyx/name task-name
:onyx/plugin :onyx.plugin.sql/write-rows
:onyx/type :output
:onyx/medium :sql
:onyx/doc "Writes segments from the :rows keys to the SQL database"}
opts)}
:schema {:task-map SqlWriterTaskMap}})
([task-name :- s/Keyword
classname :- s/Str
subprotocol :- s/Str
subname :- s/Str
user :- s/Str
password :- s/Str
db-name :- s/Str
table :- s/Keyword
copy? :- s/Bool
copy-columns :- [s/Keyword]
task-opts :- {s/Any s/Any}]
(write-rows task-name (merge {:sql/classname classname
:sql/subprotocol subprotocol
:sql/subname subname
:sql/user user
:sql/password password
:sql/db-name db-name
:sql/table table
:sql/copy? copy?
:sql/copy-columns copy-columns}
task-opts))))
(def SqlUpserterTaskMap
{:sql/classname s/Str
:sql/subprotocol s/Str
:sql/subname s/Str
:sql/user s/Str
:sql/password s/Str
:sql/db-name s/Str
:sql/table s/Keyword
(os/restricted-ns :sql) s/Any})
(s/defn ^:always-validate upsert-rows
([task-name :- s/Keyword opts]
{:task {:task-map (merge {:onyx/name task-name
:onyx/plugin :onyx.plugin.sql/upsert-rows
:onyx/type :output
:onyx/medium :sql
:onyx/doc "Writes segments from the :rows keys to the SQL database based on :where key"}
opts)}
:schema {:task-map SqlUpserterTaskMap}})
([task-name :- s/Keyword
classname :- s/Str
subprotocol :- s/Str
subname :- s/Str
user :- s/Str
password :- s/Str
db-name :- s/Str
table :- s/Keyword
task-opts :- {s/Any s/Any}]
(upsert-rows task-name (merge {:sql/classname classname
:sql/subprotocol subprotocol
:sql/subname subname
:sql/user user
:sql/password password
:sql/db-name db-name
:sql/table table}
task-opts))))