-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathget_range.py
More file actions
117 lines (94 loc) · 4.01 KB
/
Copy pathget_range.py
File metadata and controls
117 lines (94 loc) · 4.01 KB
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
# -*- coding: utf8 -*-
from example_config import *
from tablestore import *
import time
table_name = 'OTSGetRangeSimpleExample'
def create_table(client):
schema_of_primary_key = [('uid', 'INTEGER'), ('gid', 'BINARY')]
table_meta = TableMeta(table_name, schema_of_primary_key)
table_option = TableOptions()
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
client.create_table(table_meta, table_option, reserved_throughput)
print('Table has been created.')
def delete_table(client):
client.delete_table(table_name)
print('Table \'%s\' has been deleted.' % table_name)
def put_row(client):
for i in range(0, 100):
primary_key = [('uid', i), ('gid', bytearray(str(i + 1), 'utf-8'))]
attribute_columns = [('name', 'John'), ('mobile', i), ('address', 'China'), ('age', i)]
row = Row(primary_key, attribute_columns)
condition = Condition(
RowExistenceExpectation.IGNORE) # Expect not exist: put it into table only when this row is not exist.
consumed, return_row = client.put_row(table_name, row, condition)
print('Write succeed, consume %s write cu.' % consumed.write)
def get_range(client):
'''
Scan table to get all the rows.
It will not return you all once, you should continue read from next start primary key till next start primary key is None.
'''
inclusive_start_primary_key = [('uid', INF_MIN), ('gid', INF_MIN)]
exclusive_end_primary_key = [('uid', INF_MAX), ('gid', INF_MAX)]
columns_to_get = []
limit = 90
cond = CompositeColumnCondition(LogicalOperator.AND)
cond.add_sub_condition(SingleColumnCondition("address", 'China', ComparatorType.EQUAL))
cond.add_sub_condition(SingleColumnCondition("age", 50, ComparatorType.LESS_THAN))
consumed, next_start_primary_key, row_list, next_token = client.get_range(
table_name, Direction.FORWARD,
inclusive_start_primary_key, exclusive_end_primary_key,
columns_to_get,
limit,
column_filter=cond,
max_version=1
)
all_rows = []
all_rows.extend(row_list)
while next_start_primary_key is not None:
inclusive_start_primary_key = next_start_primary_key
consumed, next_start_primary_key, row_list, next_token = client.get_range(
table_name, Direction.FORWARD,
inclusive_start_primary_key, exclusive_end_primary_key,
columns_to_get, limit,
column_filter=cond,
max_version=1
)
all_rows.extend(row_list)
print('Read succeed, consume %s read cu.' % consumed.read)
for row in all_rows:
print(row.primary_key, row.attribute_columns)
print('Total rows: ', len(all_rows))
def xget_range(client):
'''
You can easily scan the range use xget_range, without handling next start primary key.
'''
consumed_counter = CapacityUnit(0, 0)
inclusive_start_primary_key = [('uid', INF_MIN), ('gid', INF_MIN)]
exclusive_end_primary_key = [('uid', INF_MAX), ('gid', INF_MAX)]
cond = CompositeColumnCondition(LogicalOperator.AND)
cond.add_sub_condition(SingleColumnCondition("address", 'China', ComparatorType.EQUAL))
cond.add_sub_condition(SingleColumnCondition("age", 50, ComparatorType.GREATER_EQUAL))
columns_to_get = []
range_iter = client.xget_range(
table_name, Direction.FORWARD,
inclusive_start_primary_key, exclusive_end_primary_key,
consumed_counter, columns_to_get, 100,
column_filter=cond, max_version=1
)
total_rows = 0
for row in range_iter:
print(row.primary_key, row.attribute_columns)
total_rows += 1
print('Total rows:', total_rows)
if __name__ == '__main__':
client = OTSClient(OTS_ENDPOINT, OTS_ACCESS_KEY_ID, OTS_ACCESS_KEY_SECRET, OTS_INSTANCE, region=OTS_REGION)
try:
delete_table(client)
except:
pass
create_table(client)
time.sleep(3) # wait for table ready
put_row(client)
get_range(client)
xget_range(client)
delete_table(client)