-
Notifications
You must be signed in to change notification settings - Fork 37
/
batch_get_row.py
90 lines (69 loc) · 3.35 KB
/
batch_get_row.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
# -*- coding: utf8 -*-
from example_config import *
from tablestore import *
import time
table_name_1 = 'OTSBatchGetRowSimpleExample_1'
table_name_2 = 'OTSBatchGetRowSimpleExample_2'
def create_table(client, table_name):
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'INTEGER')]
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, table_name):
try:
client.delete_table(table_name)
print('Table \'%s\' has been deleted.' % table_name)
except:
pass
def put_row(client, table_name):
for i in range(0, 10):
primary_key = [('gid', i), ('uid', i + 1)]
attribute_columns = [('name', 'John'), ('mobile', i), ('address', 'China'), ('age', i)]
row = Row(primary_key, attribute_columns)
condition = Condition(
RowExistenceExpectation.EXPECT_NOT_EXIST) # 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(u'Write succeed, consume %s write cu.' % consumed.write)
def batch_get_row(client):
# try to get rows from two different tables
columns_to_get = ['name', 'mobile', 'address', 'age']
rows_to_get = []
for i in range(0, 10):
primary_key = [('gid', i), ('uid', i + 1)]
rows_to_get.append(primary_key)
cond = CompositeColumnCondition(LogicalOperator.AND)
cond.add_sub_condition(SingleColumnCondition("name", "John", ComparatorType.EQUAL))
cond.add_sub_condition(SingleColumnCondition("address", 'China', ComparatorType.EQUAL))
request = BatchGetRowRequest()
request.add(TableInBatchGetRowItem(table_name_1, rows_to_get, columns_to_get, cond, 1))
request.add(TableInBatchGetRowItem(table_name_2, rows_to_get, columns_to_get, cond, 1))
result = client.batch_get_row(request)
print('Result status: %s' % (result.is_all_succeed()))
table_result_0 = result.get_result_by_table(table_name_1)
table_result_1 = result.get_result_by_table(table_name_2)
print('Check first table\'s result:')
for item in table_result_0:
if item.is_ok:
print('Read succeed, PrimaryKey: %s, Attributes: %s' % (item.row.primary_key, item.row.attribute_columns))
else:
print('Read failed, error code: %s, error message: %s' % (item.error_code, item.error_message))
print('Check second table\'s result:')
for item in table_result_1:
if item.is_ok:
print('Read succeed, PrimaryKey: %s, Attributes: %s' % (item.row.primary_key, item.row.attribute_columns))
else:
print('Read failed, error code: %s, error message: %s' % (item.error_code, item.error_message))
if __name__ == '__main__':
client = OTSClient(OTS_ENDPOINT, OTS_ACCESS_KEY_ID, OTS_ACCESS_KEY_SECRET, OTS_INSTANCE)
delete_table(client, table_name_1)
delete_table(client, table_name_2)
create_table(client, table_name_1)
create_table(client, table_name_2)
time.sleep(3) # wait for table ready
put_row(client, table_name_1)
put_row(client, table_name_2)
batch_get_row(client)
delete_table(client, table_name_1)
delete_table(client, table_name_2)