-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbase.go
418 lines (394 loc) · 12.1 KB
/
hbase.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Package gohbase provides a pool of hbase clients
package gohbase
import "github.com/tianxingpan/gohbase/hbase"
type HBase interface {
// Test for the existence of columns in the table, as specified in the TGet.
//
// @return true if the specified TGet matches one or more keys, false if not
//
// Parameters:
// - Table: the table to check on
// - Tget: the TGet to check for
Exists(table []byte, tget *hbase.TGet) (r bool, err error)
// Method for getting data from a row.
//
// If the row cannot be found an empty Result is returned.
// This can be checked by the empty field of the TResult
//
// @return the result
//
// Parameters:
// - Table: the table to get from
// - Tget: the TGet to fetch
Get(table []byte, tget *hbase.TGet) (r *hbase.TResult_, err error)
// Method for getting multiple rows.
//
// If a row cannot be found there will be a null
// value in the result list for that TGet at the
// same position.
//
// So the Results are in the same order as the TGets.
//
// Parameters:
// - Table: the table to get from
// - Tgets: a list of TGets to fetch, the Result list
// will have the Results at corresponding positions
// or null if there was an error
GetMultiple(table []byte, tgets []*hbase.TGet) (r []*hbase.TResult_, err error)
// Commit a TPut to a table.
//
// Parameters:
// - Table: the table to put data in
// - Tput: the TPut to put
Put(table []byte, tput *hbase.TPut) (err error)
// Atomically checks if a row/family/qualifier value matches the expected
// value. If it does, it adds the TPut.
//
// @return true if the new put was executed, false otherwise
//
// Parameters:
// - Table: to check in and put to
// - Row: row to check
// - Family: column family to check
// - Qualifier: column qualifier to check
// - Value: the expected value, if not provided the
// check is for the non-existence of the
// column in question
// - Tput: the TPut to put if the check succeeds\
CheckAndPut(table, row, family, qualifier, value []byte, tput *hbase.TPut) (r bool, err error)
// Commit a List of Puts to the table.
//
// Parameters:
// - Table: the table to put data in
// - Tputs: a list of TPuts to commit
PutMultiple(table []byte, tputs []*hbase.TPut) (err error)
// Deletes as specified by the TDelete.
//
// Note: "delete" is a reserved keyword and cannot be used in Thrift
// thus the inconsistent naming scheme from the other functions.
//
// Parameters:
// - Table: the table to delete from
// - Tdelete: the TDelete to delete
DeleteSingle(table []byte, tdelete *hbase.TDelete) (err error)
// Bulk commit a List of TDeletes to the table.
//
// Throws a TIOError if any of the deletes fail.
//
// Always returns an empty list for backwards compatibility.
//
// Parameters:
// - Table: the table to delete from
// - Tdeletes: list of TDeletes to delete
DeleteMultiple(table []byte, tdeletes []*hbase.TDelete) (r []*hbase.TDelete, err error)
// Atomically checks if a row/family/qualifier value matches the expected
// value. If it does, it adds the delete.
//
// @return true if the new delete was executed, false otherwise
//
// Parameters:
// - Table: to check in and delete from
// - Row: row to check
// - Family: column family to check
// - Qualifier: column qualifier to check
// - Value: the expected value, if not provided the
// check is for the non-existence of the
// column in question
// - Tdelete: the TDelete to execute if the check succeeds
CheckAndDelete(table, row, family, qualifier, value []byte, tdelete *hbase.TDelete) (r bool, err error)
// Parameters:
// - Table: the table to increment the value on
// - Tincrement: the TIncrement to increment
Increment(table []byte, tincrement *hbase.TIncrement) (r *hbase.TResult_, err error)
// Parameters:
// - Table: the table to append the value on
// - Tappend: the TAppend to append
Append(table []byte, tappend *hbase.TAppend) (r *hbase.TResult_, err error)
// Get a Scanner for the provided TScan object.
//
// @return Scanner Id to be used with other scanner procedures
//
// Parameters:
// - Table: the table to get the Scanner for
// - Tscan: the scan object to get a Scanner for
OpenScanner(table []byte, tscan *hbase.TScan) (r int32, err error)
// Grabs multiple rows from a Scanner.
//
// @return Between zero and numRows TResults
//
// Parameters:
// - ScannerId: the Id of the Scanner to return rows from. This is an Id returned from the openScanner function.
// - NumRows: number of rows to return
GetScannerRows(scannerId int32, numRows int32) (r []*hbase.TResult_, err error)
// Closes the scanner. Should be called to free server side resources timely.
// Typically close once the scanner is not needed anymore, i.e. after looping
// over it to get all the required rows.
//
// Parameters:
// - ScannerId: the Id of the Scanner to close *
CloseScanner(scannerId int32) (err error)
// mutateRow performs multiple mutations atomically on a single row.
//
// Parameters:
// - Table: table to apply the mutations
// - TrowMutations: mutations to apply
MutateRow(table []byte, trowMutations *hbase.TRowMutations) (err error)
// Get results for the provided TScan object.
// This helper function opens a scanner, get the results and close the scanner.
//
// @return between zero and numRows TResults
//
// Parameters:
// - Table: the table to get the Scanner for
// - Tscan: the scan object to get a Scanner for
// - NumRows: number of rows to return
GetScannerResults(table []byte, tscan *hbase.TScan, numRows int32) (r []*hbase.TResult_, err error)
// Given a table and a row get the location of the region that
// would contain the given row key.
//
// reload = true means the cache will be cleared and the location
// will be fetched from meta.
//
// Parameters:
// - Table
// - Row
// - Reload
GetRegionLocation(table, row []byte, reload bool) (r *hbase.THRegionLocation, err error)
// Get all of the region locations for a given table.
//
//
// Parameters:
// - Table
GetAllRegionLocations(table []byte) (r []*hbase.THRegionLocation, err error)
// Close HBase client
Close() (err error)
}
func NewHBase(opt *Options) HBase {
return &hBaseCMD{
opt: opt,
thriftConnPool: NewThriftConnPool(opt),
}
}
type hBaseCMD struct {
opt *Options
thriftConnPool *ThriftConnPool
}
// Append implements HBase
func (h *hBaseCMD) Append(table []byte, tappend *hbase.TAppend) (r *hbase.TResult_, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.Append(table, tappend)
return
}
// CheckAndDelete implements HBase
func (h *hBaseCMD) CheckAndDelete(table []byte, row []byte, family []byte, qualifier []byte, value []byte, tdelete *hbase.TDelete) (r bool, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.CheckAndDelete(table, row, family, qualifier, value, tdelete)
return
}
// CheckAndPut implements HBase
func (h *hBaseCMD) CheckAndPut(table []byte, row []byte, family []byte, qualifier []byte, value []byte, tput *hbase.TPut) (r bool, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.CheckAndPut(table, row, family, qualifier, value, tput)
return
}
// CloseScanner implements HBase
func (h *hBaseCMD) CloseScanner(scannerId int32) (err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
err = hc.CloseScanner(scannerId)
return
}
// DeleteMultiple implements HBase
func (h *hBaseCMD) DeleteMultiple(table []byte, tdeletes []*hbase.TDelete) (r []*hbase.TDelete, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.DeleteMultiple(table, tdeletes)
return
}
// DeleteSingle implements HBase
func (h *hBaseCMD) DeleteSingle(table []byte, tdelete *hbase.TDelete) (err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
err = hc.DeleteSingle(table, tdelete)
return
}
// Exists implements HBase
func (h *hBaseCMD) Exists(table []byte, tget *hbase.TGet) (r bool, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.Exists(table, tget)
return
}
// Get implements HBase
func (h *hBaseCMD) Get(table []byte, tget *hbase.TGet) (r *hbase.TResult_, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.Get(table, tget)
return
}
// GetAllRegionLocations implements HBase
func (h *hBaseCMD) GetAllRegionLocations(table []byte) (r []*hbase.THRegionLocation, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.GetAllRegionLocations(table)
return
}
// GetMultiple implements HBase
func (h *hBaseCMD) GetMultiple(table []byte, tgets []*hbase.TGet) (r []*hbase.TResult_, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.GetMultiple(table, tgets)
return
}
// GetRegionLocation implements HBase
func (h *hBaseCMD) GetRegionLocation(table []byte, row []byte, reload bool) (r *hbase.THRegionLocation, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.GetRegionLocation(table, row, reload)
return
}
// GetScannerResults implements HBase
func (h *hBaseCMD) GetScannerResults(table []byte, tscan *hbase.TScan, numRows int32) (r []*hbase.TResult_, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.GetScannerResults(table, tscan, numRows)
return
}
// GetScannerRows implements HBase
func (h *hBaseCMD) GetScannerRows(scannerId int32, numRows int32) (r []*hbase.TResult_, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.GetScannerRows(scannerId, numRows)
return
}
// Increment implements HBase
func (h *hBaseCMD) Increment(table []byte, tincrement *hbase.TIncrement) (r *hbase.TResult_, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.Increment(table, tincrement)
return
}
// MutateRow implements HBase
func (h *hBaseCMD) MutateRow(table []byte, trowMutations *hbase.TRowMutations) (err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
err = hc.MutateRow(table, trowMutations)
return
}
// OpenScanner implements HBase
func (h *hBaseCMD) OpenScanner(table []byte, tscan *hbase.TScan) (r int32, err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
r, err = hc.OpenScanner(table, tscan)
return
}
// Put implements HBase
func (h *hBaseCMD) Put(table []byte, tput *hbase.TPut) (err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
err = hc.Put(table, tput)
return
}
// PutMultiple implements HBase
func (h *hBaseCMD) PutMultiple(table []byte, tputs []*hbase.TPut) (err error) {
var cn *ThriftConn
cn, err = h.thriftConnPool.Get()
if err != nil {
return
}
defer h.thriftConnPool.Put(cn)
hc := cn.GetHbaseClient()
err = hc.PutMultiple(table, tputs)
return
}
func (h *hBaseCMD) Close() error {
return h.thriftConnPool.Close()
}