-
Notifications
You must be signed in to change notification settings - Fork 37
/
batch_write_row.py
89 lines (72 loc) · 3.32 KB
/
batch_write_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
# -*- coding: utf8 -*-
from example_config import *
from tablestore import *
import time
table_name = 'OTSBatchWriteRowSimpleExample'
def create_table(client):
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'INTEGER')]
table_meta = TableMeta(table_name, schema_of_primary_key)
table_options = TableOptions()
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
client.create_table(table_meta, table_options, 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 batch_write_row(client):
# batch put 10 rows and update 10 rows on exist table, delete 10 rows on a not-exist table.
put_row_items = []
for i in range(0, 10):
primary_key = [('gid', i), ('uid', i + 1)]
attribute_columns = [('name', 'somebody' + str(i)), ('address', 'somewhere' + str(i)), ('age', i)]
row = Row(primary_key, attribute_columns)
condition = Condition(RowExistenceExpectation.IGNORE)
item = PutRowItem(row, condition)
put_row_items.append(item)
for i in range(10, 20):
primary_key = [('gid', i), ('uid', i + 1)]
attribute_columns = {'put': [('name', 'somebody' + str(i)), ('address', 'somewhere' + str(i)), ('age', i)]}
row = Row(primary_key, attribute_columns)
condition = Condition(RowExistenceExpectation.IGNORE, SingleColumnCondition("age", i, ComparatorType.EQUAL))
item = UpdateRowItem(row, condition)
put_row_items.append(item)
delete_row_items = []
for i in range(10, 20):
primary_key = [('gid', i), ('uid', i + 1)]
row = Row(primary_key)
condition = Condition(RowExistenceExpectation.IGNORE)
item = DeleteRowItem(row, condition)
delete_row_items.append(item)
request = BatchWriteRowRequest()
request.add(TableInBatchWriteRowItem(table_name, put_row_items))
request.add(TableInBatchWriteRowItem('notExistTable', delete_row_items))
result = client.batch_write_row(request)
print('Result status: %s' % (result.is_all_succeed()))
print('check first table\'s put results:')
succ, fail = result.get_put()
for item in succ:
print('Put succeed, consume %s write cu.' % item.consumed.write)
for item in fail:
print('Put failed, error code: %s, error message: %s' % (item.error_code, item.error_message))
print('check first table\'s update results:')
succ, fail = result.get_update()
for item in succ:
print('Update succeed, consume %s write cu.' % item.consumed.write)
for item in fail:
print('Update failed, error code: %s, error message: %s' % (item.error_code, item.error_message))
print('check second table\'s delete results:')
succ, fail = result.get_delete()
for item in succ:
print('Delete succeed, consume %s write cu.' % item.consumed.write)
for item in fail:
print('Delete 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)
try:
delete_table(client)
except:
pass
create_table(client)
time.sleep(3) # wait for table ready
batch_write_row(client)
delete_table(client)