forked from tolitius/cbass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbass.clj
233 lines (208 loc) · 8.51 KB
/
cbass.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(ns cbass
(:require [taoensso.nippy :as n]
[cbass.scan :refer [scan-filter]]
[cbass.tools :refer [to-bytes thaw no-values]])
(:import [java.util ArrayList Collection List]
[org.apache.hadoop.hbase.util Bytes]
[org.apache.hadoop.hbase TableName Cell]
[org.apache.hadoop.conf Configuration]
[org.apache.hadoop.hbase.client Connection ConnectionFactory Table Get Put Delete Scan Result ResultScanner]))
(def pack (atom n/freeze))
(def unpack (atom thaw))
(defn pack-un-pack [{:keys [p u]}]
(when p (reset! pack p))
(when u (reset! unpack u)))
(defn ^Connection new-connection [conf & {:keys [pack unpack]}]
(let [configuration (Configuration.)]
(doseq [[k v] conf]
(.set configuration k (str v)))
(pack-un-pack {:p pack :u unpack})
(ConnectionFactory/createConnection configuration)))
(defn ^Table get-table [^Connection c ^String t-name]
(.getTable c (TableName/valueOf t-name)))
(defn result-key [k result-key-type]
(case result-key-type
:keyword (-> (String. ^bytes k) keyword)
:string (String. ^bytes k)
:bytes k
(throw (IllegalArgumentException. "result-key-type must be one of the following: [:bytes :string :keyword]"))))
(defn result-value [v]
(@unpack v))
(defn latest-ts [^Result result]
(let [cells (.rawCells result)]
(->> (map #(.getTimestamp ^Cell %) cells)
(apply max))))
(defn hdata->map [^Result data {:keys [result-key-type with-ts-per-cell?]
:or {result-key-type :keyword with-ts-per-cell? false}}]
(when-let [r (.getRow data)]
(let [ts (latest-ts data)
; with-ts-per-cell? forces usage in the Cells array instead of getNoVersionMap function
results (if with-ts-per-cell?
(for [cell (.rawCells data)]
(when-let [v (result-value (.getValueArray cell))]
(let [k (result-key (.getQualifierArray cell) result-key-type)]
[k (if with-ts-per-cell? {:value v :timestamp (.getTimestamp cell)} v)])))
(for [kv (-> (.getNoVersionMap data) vals first)]
(if-some [v (result-value (val kv))]
[(result-key (key kv) result-key-type) v])))]
(into {:last-updated ts} results))))
(defn map->hdata [row-key family columns timestamp]
(let [^Put p (if timestamp
(Put. ^bytes (to-bytes ^String row-key) timestamp)
(Put. ^bytes (to-bytes ^String row-key)))
^bytes f-bytes (to-bytes ^String family)]
(doseq [[k v] columns]
(when-let [v-bytes (@pack v)]
(.add p f-bytes (to-bytes (name k)) v-bytes)))
p))
(defn results->maps [results row-key-fn {:keys [keys-only?] :as opts}]
(for [^Result r results]
[(row-key-fn (.getRow r))
(if keys-only?
{:last-updated (latest-ts r)}
(hdata->map r opts))]))
(defn without-ts [results]
(for [[k v] results]
[k (dissoc v :last-updated)]))
(defn scan
"Supported keys in criteria:
- row-key-fn
- limit
- with-ts?
- keys-only?
- from
- to
- starts-with
- result-key-type - [:bytes :string :keyword]
- with-ts-per-cell?
"
[conn table & {:keys [row-key-fn limit with-ts? lazy?] :as criteria}]
(let [scan ^Scan (scan-filter criteria)]
(when limit
(.setLimit scan limit))
(with-open [^Table h-table (get-table conn table)
scanner (.getScanner h-table scan)]
(let [results (-> (.iterator scanner)
iterator-seq)
row-key-fn (or row-key-fn #(String. ^bytes %))
rmap (results->maps results
row-key-fn
criteria)]
(cond->> rmap
(not with-ts?) (without-ts)
list? (into [])
(and (not list?)
(not lazy?)) (into {}))))))
(defn lazy-scan
"
Scan rows. Return a map with:
- table: the table object
- scanner: the scanner object
- rows: A lazy sequence with the rows
IMPORTANT: It's the responsibility of the caller to close table and scanner.
Supported keys in criteria:
- row-key-fn
- limit
- with-ts?
- keys-only?
- from
- to
- starts-with
"
[conn table & {:keys [row-key-fn limit with-ts?] :as criteria}]
(let [scan ^Scan (scan-filter criteria)]
(when limit
(.setLimit scan limit))
(let [^Table h-table (get-table conn table)
^ResultScanner scanner (.getScanner h-table scan)
results (-> (.iterator scanner)
iterator-seq)
row-key-fn (or row-key-fn #(String. ^bytes %))
rmap (results->maps results
row-key-fn
criteria)]
{:table h-table
:scanner scanner
:rows (cond->> rmap
(not with-ts?) (without-ts))})))
(defn- set-time-range! [^Get get {:keys [from-ms to-ms]
:or {from-ms 0
to-ms Long/MAX_VALUE}}]
(.setTimeRange get from-ms to-ms))
(defn find-by
([conn table row-key]
(find-by conn table row-key nil nil))
([conn table row-key family]
(find-by conn table row-key family nil))
([conn table row-key family columns & {:keys [time-range] :as opts}]
(with-open [^Table h-table (get-table conn table)]
(let [^Get g (Get. ^bytes (to-bytes ^String row-key))]
(when time-range
(set-time-range! g time-range))
(when family
(if columns
(doseq [c columns]
(.addColumn g (to-bytes ^String family) (to-bytes (name c))))
(.addFamily g (to-bytes ^String family))))
(-> (.get h-table g)
(hdata->map opts))))))
(defn store
([conn table row-key family]
(with-open [^Table h-table (get-table conn table)]
(let [^Put p (Put. ^bytes (to-bytes ^String row-key))]
(.put h-table (.add p (to-bytes ^String family) ;; in case there are no columns, just store row-key and family
no-values
no-values)))))
([conn table row-key family columns]
(store conn table row-key family columns nil))
([conn table row-key family columns timestamp]
(with-open [^Table h-table (get-table conn table)]
(let [^Put h-data (map->hdata row-key family columns timestamp)]
(.put h-table h-data)))))
(defn empty-row-put [row-key family]
(let [^Put p (Put. ^bytes (to-bytes ^String row-key))]
(.add p (to-bytes ^String family)
no-values
no-values)
p))
(defn store-batch [conn table rows]
(with-open [^Table h-table (get-table conn table)]
(let [bulk (doall (for [[r f cs ts] rows]
(if cs
(map->hdata r f cs ts)
(empty-row-put r f))))]
(.put h-table ^List bulk))))
(defn delete
([conn table row-key]
(delete conn table row-key nil nil))
([conn table row-key family]
(delete conn table row-key family nil))
([conn table row-key family columns]
(with-open [^Table h-table (get-table conn table)]
(let [^Delete d (Delete. ^bytes (to-bytes ^String row-key))]
(when family
(if columns
(doseq [c columns]
(.deleteColumns d (to-bytes ^String family) (to-bytes (name c))))
(.deleteFamily d (to-bytes ^String family))))
(.delete h-table d)))))
(defn delete-by
"Delete by using scan's syntax for the selection criteria."
[conn table & by]
(let [my-scan (->> (conj by true :keys-only?)
;; fetch keys lazily to avoid OOM errors
(apply lazy-scan conn table))]
(with-open [^Table h-table (:table my-scan)
^ResultScanner _scanner (:scanner my-scan)]
(when-let [row-keys (seq (map first (:rows my-scan)))]
(let [{:keys [delete-key-fn]
:or {delete-key-fn #(to-bytes %)}} (apply hash-map by)
bulk (ArrayList. ^Collection (map #(Delete. ^bytes (delete-key-fn %)) row-keys))]
(.delete h-table ^ArrayList bulk))))))
(defn delete-column
"Delete the specified version of the specified column"
([conn table row-key family column timestamp]
(with-open [^Table h-table (get-table conn table)]
(let [^Delete d (Delete. ^bytes (to-bytes ^String row-key))]
(.addColumn d (to-bytes ^String family) (to-bytes (name column)) timestamp)
(.delete h-table d)))))