-
Notifications
You must be signed in to change notification settings - Fork 2
/
HbaseShell.py
445 lines (365 loc) · 12.4 KB
/
HbaseShell.py
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Jul 14, 2013
@author: tomr
'''
import numbers
import subprocess
STATUS_OPTIONS = ['simple', 'summary', 'detailed']
class HbaseShell(object):
'''
This class provides python access to hbase shell functionality
Implementation is based on:
http://wiki.apache.org/hadoop/Hbase/Shell
Does not support java class filters on scan commands, e.g.,
hbase> scan 't1', {FILTER => org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)}
'''
# Utils functions for inner usage
@staticmethod
def exectueCmd(cmd):
p1 = subprocess.Popen(['echo', cmd], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['hbase', 'shell'], stdin=p1.stdout,
stdout=subprocess.PIPE)
p1.stdout.close()
output = []
line = p2.stdout.readline()
while line:
output.append(line.strip())
line = p2.stdout.readline()
p2.stdout.close()
return output
@staticmethod
def argsDataToHbaseArgsString(dataArgs):
if not dataArgs:
return ''
if type(dataArgs) == type({}):
hbaseDictString = '{' + ', '.join([k.upper() + ' => '
+ HbaseShell.argsDataToHbaseArgsString(v) for (k,
v) in dataArgs.iteritems() if v != None]) + '}'
return (hbaseDictString if len(hbaseDictString) > 2 else '')
if type(dataArgs) == str:
if "'" in dataArgs:
return '"' + dataArgs + '"'
return "'" + dataArgs + "'"
if isinstance(dataArgs, numbers.Integral):
return str(dataArgs)
if type(dataArgs) == bool:
return str(dataArgs).lower()
if type(dataArgs) == type([]):
return '[' \
+ ', '.join([HbaseShell.argsDataToHbaseArgsString(v)
for v in dataArgs]) + ']'
@staticmethod
def ddlFuncParamtersToStr(
cmdName,
table,
columns=None,
args=None,
):
cmd = cmdName + "'" + table + "'"
columnsStr = \
', '.join([HbaseShell.argsDataToHbaseArgsString(cf)
for cf in columns])
additioalDataStr = HbaseShell.argsDataToHbaseArgsString(args)
if columnsStr != '' or additioalDataStr != '':
cmd += ', ' + ', '.join([dataStr for dataStr in
[columnsStr, additioalDataStr]
if dataStr != ''])
return HbaseShell.exectueCmd(cmd)
# Hbase shell general commands
@staticmethod
def status(st=None):
cmd = 'status'
if st != None:
if st not in STATUS_OPTIONS:
raise ValueError('Invalid status option, legal values: '
+ str(STATUS_OPTIONS))
else:
cmd += " '" + st + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def version():
cmd = 'version'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def whoami():
cmd = 'whoami'
return HbaseShell.exectueCmd(cmd)
# Hbase shell ddl commands
@staticmethod
def alter(table, columns=None, args=None):
'''
parameters -
table - table name
columns - list of dictionaries which describes the column families:
{name : column-family name (obligatory), version : number of versions to keep, int (optional),
ttl : time to live, long (optional), etc. all the possible \
properties a column may get}
args - all the general parameters alter can get
'''
return HbaseShell.ddlFuncParamtersToStr('create', table,
columns, args)
@staticmethod
def alter_async(table, columns=None, args=None):
'''
parameters -
table - table name
columns - list of dictionaries which describes the column families:
{name : column-family name (obligatory), version : number of versions to keep, int (optional),
ttl : time to live, long (optional), etc. all the possible \
properties a column may get}
args - all the general parameters alter can get
'''
return HbaseShell.ddlFuncParamtersToStr('alter_async', table,
columns, args)
@staticmethod
def alter_status(table):
cmd = "alter_status '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def create(table, columns=None, args=None):
'''
parameters -
table - table name
columns - list of dictionaries which describes the column families:
{name : column-family name (obligatory), version : number of versions to keep, int (optional),
ttl : time to live, long (optional), blockcache : boolean (optional), etc. all the possible \
properties a column may get}
args - all the general parameters create can get
'''
return HbaseShell.ddlFuncParamtersToStr('create', table,
columns, args)
@staticmethod
def describe(table):
cmd = "describe '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def disable(table):
cmd = "disable '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def disable_all():
cmd = 'disable_all'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def drop(table):
cmd = "drop '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def drop_all():
cmd = 'drop_all'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def enable(table):
cmd = "enable '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def enable_all():
cmd = 'enable_all'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def exists(table):
cmd = "exists '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def get_table(table):
cmd = "get_table '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def is_disabled(table):
cmd = "is_disabled '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def is_enabled(table):
cmd = "is_enabled '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def list():
cmd = 'list'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def show_filters(table):
cmd = 'show_filters'
return HbaseShell.exectueCmd(cmd)
# Hbase shell dml commands
@staticmethod
def count(table, args=None):
cmd = "count '" + table + "'"
dataStr = HbaseShell.argsDataToHbaseArgsString(args)
if dataStr != '':
cmd += ', ' + dataStr
return HbaseShell.exectueCmd(cmd)
@staticmethod
def delete(
table,
row,
column,
value,
timestamp=None,
):
cmd = "delete '" + table + "', '" + row + "', '" + column \
+ "', '" + value + "'"
if timestamp != None:
if isinstance(timestamp, numbers.Integral):
cmd += ", '" + str(timestamp) + "'"
else:
raise ValueError('timestamp must be integral')
return HbaseShell.exectueCmd(cmd)
@staticmethod
def deleteall(
table,
row,
column=None,
timestamp=None,
):
cmd = "deleteall '" + table + "', '" + row
if column != None:
cmd += "', '" + column + "'"
if timestamp != None:
if isinstance(timestamp, numbers.Integral):
cmd += ", '" + str(timestamp) + "'"
else:
raise ValueError('timestamp must be integral')
return HbaseShell.exectueCmd(cmd)
@staticmethod
def get(table, row, args=None):
cmd = "get '" + table + "', '" + row + "' "
dataStr = HbaseShell.argsDataToHbaseArgsString(args)
if dataStr != '':
cmd += ', ' + dataStr
return HbaseShell.exectueCmd(cmd)
@staticmethod
def get_counter(table, row, column):
cmd = "get_counter '" + table + "', " + row + "', " + column \
+ "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def incr(
table,
row,
column,
value=None,
):
cmd = "incr '" + table + "', " + row + "', " + column + "'"
if value != None:
cmd += ', ' + str(value)
return HbaseShell.exectueCmd(cmd)
@staticmethod
def put(
table,
row,
column,
value,
timestamp=None,
):
cmd = "put '" + table + "', '" + row + "', '" + column + "', '" \
+ str(value)
if timestamp != None:
if isinstance(timestamp, numbers.Integral):
cmd += "', '" + str(timestamp)
else:
raise ValueError('timestamp must be integral')
return HbaseShell.exectueCmd(cmd)
@staticmethod
def scan(table, args=None):
cmd = "scan '" + table + "'"
dataStr = HbaseShell.argsDataToHbaseArgsString(args)
if dataStr != '':
cmd += ', ' + dataStr
return HbaseShell.exectueCmd(cmd)
@staticmethod
def truncate(table):
cmd = "truncate '" + table + "'"
return HbaseShell.exectueCmd(cmd)
# Hbase shell tools commands
@staticmethod
def tools():
cmd = 'tools'
return HbaseShell.exectueCmd(cmd)
# Hbase shell replication commands
@staticmethod
def add_peer(peer_id, cluster):
cmd = "add_peer '" + peer_id + '\', "' + cluster + '"'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def disable_peer(peer_id):
cmd = "disable_peer '" + peer_id + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def enable_peer(peer_id):
cmd = "enable_peer '" + peer_id + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def list_peers(peer_id):
cmd = 'list_peers'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def remove_peer(peer_id):
cmd = "remove_peer '" + peer_id + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def start_replication(peer_id):
cmd = 'start_replication'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def stop_replication(peer_id):
cmd = 'stop_replication'
return HbaseShell.exectueCmd(cmd)
# Hbase shell snapshot commands
@staticmethod
def clone_snapshot(snapshot, table):
cmd = "clone_snapshot '" + snapshot + "', '" + table + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def delete_snapshot(snapshot):
cmd = "delete_snapshot '" + snapshot + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def list_snapshots():
cmd = 'list_snapshots'
return HbaseShell.exectueCmd(cmd)
@staticmethod
def restore_snapshot(snapshot):
cmd = "restore_snapshot '" + snapshot + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def snapshot(table, snapshot):
cmd = "snapshot '" + table + "', '" + snapshot + "'"
return HbaseShell.exectueCmd(cmd)
# Hbase shell security commands
@staticmethod
def grant(
user,
permissions,
table=None,
columnFamily=None,
columnQualifier=None,
):
cmd = "grant '" + user + "', " + permissions + "'"
if table != None:
cmd += ", '" + table + "'"
if columnFamily != None:
cmd += ", '" + columnFamily + "'"
if columnQualifier != None:
cmd += ", '" + columnQualifier + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def revoke(
user,
table=None,
columnFamily=None,
columnQualifier=None,
):
cmd = "revoke '" + user + "'"
if table != None:
cmd += ", '" + table + "'"
if columnFamily != None:
cmd += ", '" + columnFamily + "'"
if columnQualifier != None:
cmd += ", '" + columnQualifier + "'"
return HbaseShell.exectueCmd(cmd)
@staticmethod
def user_permission(table):
cmd = "user_permission '" + table + "'"
return HbaseShell.exectueCmd(cmd)