Skip to content

Commit 3f2bc83

Browse files
committed
New feature: Global secondary index.
1 parent c7110dd commit 3f2bc83

File tree

11 files changed

+425
-130
lines changed

11 files changed

+425
-130
lines changed

examples/search_index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def prepare_index(index_name, with_nested=False):
211211
fields.append(field_n)
212212
index_setting = IndexSetting(routing_fields=['PK1'])
213213
index_sort = Sort(sorters=[PrimaryKeySort(SortOrder.ASC)]) if not with_nested else None
214-
index_meta = IndexMeta(fields, index_setting=index_setting, index_sort=index_sort) # default with index sort
214+
index_meta = SearchIndexMeta(fields, index_setting=index_setting, index_sort=index_sort) # default with index sort
215215
client.create_search_index(table_name, index_name, index_meta)
216216

217217
def list_search_index():
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -*- coding: utf8 -*-
2+
3+
from example_config import *
4+
from tablestore import *
5+
import time
6+
import json
7+
8+
table_name = 'SecondaryIndexOperationExample'
9+
10+
def create_table(client):
11+
print ('Begin CreateTable')
12+
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'STRING')]
13+
defined_columns = [('i', 'INTEGER'), ('bool', 'BOOLEAN'), ('d', 'DOUBLE'), ('s', 'STRING'), ('b', 'BINARY')]
14+
table_meta = TableMeta(table_name, schema_of_primary_key, defined_columns)
15+
table_option = TableOptions(-1, 1)
16+
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
17+
secondary_indexes = [
18+
SecondaryIndexMeta('index1', ['i', 's'], ['gid', 'uid', 'bool', 'b', 'd']),
19+
]
20+
client.create_table(table_meta, table_option, reserved_throughput, secondary_indexes)
21+
print ('Table has been created.')
22+
23+
def create_index(client):
24+
print ('Begin CreateIndex')
25+
index_meta = SecondaryIndexMeta('index2', ['i', 's'], ['gid', 'uid', 'bool', 'b', 'd'])
26+
client.create_secondary_index(table_name, index_meta)
27+
print ('Index has been created.')
28+
29+
def describe_table(client):
30+
print ('Begin DescribeTable')
31+
describe_response = client.describe_table(table_name)
32+
print ('TableName: %s' % describe_response.table_meta.table_name)
33+
print ('PrimaryKey: %s' % describe_response.table_meta.schema_of_primary_key)
34+
print ('Reserved read throughput: %s' % describe_response.reserved_throughput_details.capacity_unit.read)
35+
print ('Reserved write throughput: %s' % describe_response.reserved_throughput_details.capacity_unit.write)
36+
print ('Last increase throughput time: %s' % describe_response.reserved_throughput_details.last_increase_time)
37+
print ('Last decrease throughput time: %s' % describe_response.reserved_throughput_details.last_decrease_time)
38+
print ('table options\'s time to live: %s' % describe_response.table_options.time_to_live)
39+
print ('table options\'s max version: %s' % describe_response.table_options.max_version)
40+
print ('table options\'s max_time_deviation: %s' % describe_response.table_options.max_time_deviation)
41+
print ('Secondary indexes:')
42+
for secondary_index in describe_response.secondary_indexes:
43+
print (json.dumps(secondary_index, default=lambda x:x.__dict__, indent=2))
44+
print ('End DescribeTable')
45+
46+
def delete_index(client):
47+
print ('Begin DeleteIndex')
48+
client.delete_secondary_index(table_name, 'index1')
49+
print ('End delete index.')
50+
51+
def delete_table(client):
52+
print ('Begin DeleteTable')
53+
client.delete_table(table_name)
54+
print ('End DeleteTable')
55+
56+
if __name__ == '__main__':
57+
client = OTSClient(OTS_ENDPOINT, OTS_ID, OTS_SECRET, OTS_INSTANCE)
58+
try:
59+
delete_table(client)
60+
print 'delete succeeded.'
61+
except:
62+
pass
63+
64+
create_table(client)
65+
66+
#time.sleep(3) # wait for table ready
67+
create_index(client)
68+
describe_table(client)
69+
delete_index(client)
70+
describe_table(client)
71+

tablestore/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
'CompositeColumnCondition',
4545
'SingleColumnCondition',
4646
'RowExistenceExpectation',
47-
'IndexMeta',
47+
'SearchIndexMeta',
4848
'FieldSchema',
4949
'FieldType',
5050
'IndexSetting',
@@ -79,7 +79,10 @@
7979
'ColumnReturnType',
8080
'FieldValueFactor',
8181
'GeoDistanceType',
82-
'NestedFilter'
82+
'NestedFilter',
83+
'DefinedColumnSchema',
84+
'SecondaryIndexMeta',
85+
'SecondaryIndexType',
8386
]
8487

8588

tablestore/client.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _request_helper(self, api_name, *args, **kwargs):
164164

165165
return self.protocol.parse_response(api_name, status, resheaders, resbody)
166166

167-
def create_table(self, table_meta, table_options, reserved_throughput):
167+
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[]):
168168
"""
169169
说明:根据表信息创建表。
170170
@@ -174,6 +174,7 @@ def create_table(self, table_meta, table_options, reserved_throughput):
174174
``table_options``是``tablestore.metadata.TableOptions``类的示例,它包含time_to_live,max_version和
175175
max_time_deviation三个参数。
176176
``reserved_throughput``是``tablestore.metadata.ReservedThroughput``类的实例,表示预留读写吞吐量。
177+
``secondary_indexes``是一个数组,可以包含一个或多个``tablestore.metadata.SecondaryIndexMeta``类的实例,表示要创建的二级索引。
177178
178179
返回:无。
179180
@@ -186,7 +187,7 @@ def create_table(self, table_meta, table_options, reserved_throughput):
186187
client.create_table(table_meta, table_options, reserved_throughput)
187188
"""
188189

189-
self._request_helper('CreateTable', table_meta, table_options, reserved_throughput)
190+
self._request_helper('CreateTable', table_meta, table_options, reserved_throughput, secondary_indexes)
190191

191192
def delete_table(self, table_name):
192193
"""
@@ -645,7 +646,7 @@ def create_search_index(self, table_name, index_name, index_meta):
645646
:type index_name: str
646647
:param index_name: The name of index.
647648
648-
:type index_meta: tablestore.metadata.IndexMeta
649+
:type index_meta: tablestore.metadata.SearchIndexMeta
649650
:param index_meta: The definition of index, includes fields' schema, index setting and index pre-sorting configuration.
650651
651652
Example usage:
@@ -661,7 +662,7 @@ def create_search_index(self, table_name, index_name, index_meta):
661662
])
662663
fields = [field_a, field_b, field_c, field_d, nested_field]
663664
664-
index_meta = IndexMeta(fields, index_setting=None, index_sort=None)
665+
index_meta = SearchIndexMeta(fields, index_setting=None, index_sort=None)
665666
client.create_search_index('table_1', 'index_1', index_meta)
666667
"""
667668

@@ -713,3 +714,35 @@ def search(self, table_name, index_name, search_query, columns_to_get=None, rout
713714

714715
return self._request_helper('Search', table_name, index_name, search_query, columns_to_get, routing_keys)
715716

717+
def create_secondary_index(self, table_name, index_meta):
718+
"""
719+
Create a new secondary index.
720+
721+
:type table_name: str
722+
:param table_name: The name of table.
723+
724+
:type index_meta: tablestore.metadata.SecondaryIndexMeta
725+
:param index_meta: The definition of index.
726+
727+
Example usage:
728+
index_meta = SecondaryIndexMeta('index1', ['i', 's'], ['gid', 'uid', 'bool', 'b', 'd'])
729+
client.create_secondary_index(table_name, index_meta)
730+
"""
731+
732+
return self._request_helper('CreateIndex', table_name, index_meta)
733+
734+
def delete_secondary_index(self, table_name, index_name):
735+
"""
736+
Delete the secondary index.
737+
738+
:type table_name: str
739+
:param table_name: The name of table.
740+
741+
:type index_name: str
742+
:param index_name: The name of index.
743+
744+
Example usage:
745+
client.delete_secondary_index(table_name, index_name)
746+
"""
747+
748+
return self._request_helper('DropIndex', table_name, index_name)

0 commit comments

Comments
 (0)