-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtable.py
482 lines (379 loc) · 12.7 KB
/
table.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python3
"""
@author: xi
@since: 2018-04-14
"""
import os
from collections import deque
from . import client
from . import stream_io
from .client import filters
class Table(object):
def __init__(self,
namespace,
name,
write_batch_size,
read_batch_size):
"""Table object.
Args:
namespace (hbase.namespace.Namespace): Namespace object.
name (str): Table name.
write_batch_size (int): Batch size for batch_put().
read_batch_size (int): Batch size for scan().
"""
self._namespace = namespace
self._name = name
self._write_batch_size = write_batch_size
self._read_batch_size = read_batch_size
self._full_name = namespace.prefix + name
self._conn = namespace.connection
self._client = namespace.client
@property
def connection(self):
return self._conn
@property
def name(self):
return self._name
@property
def write_batch_size(self):
return self._write_batch_size
@write_batch_size.setter
def write_batch_size(self, value):
"""Setter of write_batch_size.
Args:
value (int): New value.
"""
if value <= 0:
raise ValueError('write_batch_size should be positive value.')
self._write_batch_size = value
@property
def read_batch_size(self):
return self._read_batch_size
@read_batch_size.setter
def read_batch_size(self, value):
"""Setter of read_batch_size.
Args:
value (int): New value.
"""
if value <= 0:
raise ValueError('read_batch_size should be positive value.')
self._read_batch_size = value
@property
def full_name(self):
return self._full_name
@property
def client(self):
"""Client object.
Returns:
client.Client: Client object.
"""
return self._client
def get(self, key, columns=None, filter_=None):
"""Get a row with the row key.
Args:
key (str): Row key.
columns (tuple[str]|list[str]): Columns to get.
filter_ (client.filters.Filter): Filter object.
Returns:
client.Row: The row object.
None: The row does not exist.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
return self._client.get(self._full_name, key, columns, filter_)
def get_one(self, key_only=False):
"""Get the first rows sample from the table.
Args:
key_only (bool): Only return column keys. Contents are replaced with b''.
Returns:
Row: The first row in the table.
None: The row does not exist.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
filter_ = filters.KeyOnlyFilter() if key_only else None
return self._client.get_one(self._full_name, filter_=filter_)
def scan(self,
start_row=None,
end_row=None,
columns=None,
filter_=None,
batch_size=None):
"""Scan the table.
Args:
start_row (str): Start rwo key.
end_row (str): End row key.
columns (tuple[str]|list[str]): Columns.
filter_ (hbase.filters.Filter): Filter.
batch_size (int): Max number of rows in each request.
None means use the table's read_batch_size.
Returns:
Cursor: Cursor object if success.
"""
scanner = self._client.create_scanner(
self._full_name,
start_key=start_row,
end_key=end_row,
columns=columns,
filter_=filter_,
num_rows=batch_size if batch_size is not None else self._read_batch_size
)
return Cursor(self, scanner)
def count(self,
start_row=None,
end_row=None,
verbose=None,
verbose_interval=1000):
"""Count the number of the rows in the table.
Args:
start_row (str): Start rwo key.
end_row (str): End row key.
verbose ((int, hbase.client.Row) -> T): Callback to notify the counting progress.
verbose_interval (int): Interval counts between verbose calls.
Returns:
int: The number of rows.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
count = 0
if verbose is None:
for _ in self.scan(
start_row=start_row,
end_row=end_row,
filter_=filters.KeyOnlyFilter(),
batch_size=500
):
count += 1
else:
for row in self.scan(
start_row=start_row,
end_row=end_row,
filter_=filters.KeyOnlyFilter(),
batch_size=500
):
count += 1
if count % verbose_interval == 0:
verbose(count, row)
return count
def put(self, row, callback=None):
"""Put one row into the table.
Args:
row (hbase.client.Row): Row object.
callback (callable|None): Callback when the put operation complete.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
self._conn.threads.put_task(
self._client.put,
(self._full_name, row),
callback
)
def check_and_put(self,
row,
check_column=None,
check_value=None):
"""Put one row to the table.
Atomically checks if a row/family/qualifier value matches the expected value.
If it does, it adds the put.
If the passed value is None(or b''), the check is for the lack of column (ie: non-existance)
Args:
row (hbase.client.Row): Row to put.
check_column (str): Column to check.
check_value (bytes): Valur to check.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
return self._client.check_and_put(
self._full_name,
row,
check_column,
check_value
)
def delete(self, key):
"""Delete a row by key.
Args:
key (str): Row key.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
self._client.delete(self._full_name, key)
def stream_writer(self,
filename,
column='cf:chunk',
chunk_size=8388608):
"""Create a stream writer.
Args:
filename (str): Filename(identifier) the writer will write data to.
column (str): Column that the writer store the data.
chunk_size (int): Chunk size.
Returns:
stream_io.StreamWriter: The stream writer if success.
"""
return stream_io.StreamWriter(self, filename, column, chunk_size)
def stream_reader(self, filename, column='cf:chunk'):
"""Create a stream reader.
Args:
filename (str): Filename(identifier) in the table.
column (str): Column that the reader reads data from.
Returns:
stream_io.StreamReader: The stream reader if success.
"""
return stream_io.StreamReader(self, filename, column)
def write_bytes(self,
data,
filename,
column='cf:chunk',
chunk_size=8388608):
"""Write bytes as a file to the table.
Args:
data (bytes): Data to write.
filename (str): Filename(identifier) the writer will write data to.
column (str): Column that the writer store the data.
chunk_size (int): Chunk size.
"""
with self.stream_writer(filename, column, chunk_size) as f:
f.write(data)
def read_bytes(self, filename, column='cf:chunk'):
"""Read bytes from the table.
Args:
filename (str): Filename(identifier) the reader will read from.
column (str): Column that the reader reads data.
Returns:
bytes: All bytes of the file if success.
"""
with self.stream_reader(filename, column) as f:
return f.read()
def write_file(self,
file_path,
filename=None,
column='cf:chunk',
chunk_size=8388608):
"""Write file to table.
Args:
file_path (str): File path.
filename (str): Filename(identifier) the reader will read from.
column (str): Column that the reader reads data.
chunk_size (int): Chunk size.
Raises:
IOError: Failed to open the file.
"""
if filename is None:
filename = os.path.basename(file_path)
with open(file_path, 'rb') as f:
with self.stream_writer(filename, column, chunk_size) as f1:
while True:
data = f.read(chunk_size)
if not data:
break
f1.write(data)
def read_file(self,
file_path,
filename,
column='cf:chunk',
buffer_size=8388608):
"""Read from the table and store the data to the file.
Args:
file_path (str): File path.
filename (str): Filename(identifier) the reader will read from.
column (str): Column that the reader reads data.
buffer_size (int): Buffer size.
Raises:
IOError: Failed to open the file.
"""
with self.stream_reader(filename, column) as reader:
with open(file_path, 'wb') as f:
while True:
data = reader.read(buffer_size)
if not data:
break
f.write(data)
class Cursor(object):
def __init__(self, table, scanner):
"""Cursor object.
Args:
table (Table): Table object.
scanner (client.Scanner): Scanner object.
"""
self._table = table
self.scanner = scanner
self._client = table.client
self._full_name = table.full_name
self._buffer = deque()
def __del__(self):
self.close()
def close(self):
if self.scanner is not None:
self._client.delete_scanner(self.scanner)
self.scanner = None
def next(self):
"""Get next row.
Returns:
hbase.client.Row: Row object.
None: If all rows have been iterated.
Raises:
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
while len(self._buffer) == 0:
batch = self._client.iter_scanner(self.scanner)
if batch is None:
self.close()
return None
self._buffer.extend(batch)
return self._buffer.popleft()
def __iter__(self):
return self
def __next__(self):
"""Get next row.
Returns:
client.Row: Row object.
Raises:
StopIteration: If all rows have been iterated.
RegionError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
while len(self._buffer) == 0:
batch = self._client.iter_scanner(self.scanner)
if batch is None:
self.close()
raise StopIteration()
self._buffer.extend(batch)
return self._buffer.popleft()