准备工作
在您开始Tablestore SDK开发前,需确保已开通表格存储服务并且已创建表格存储实例。
您需要提前获取到以下几个参数
开发简介
开发示例中将以订单场景为例,使用Tablestore SDK实现如下几个功能。
- 订单表创建。
- 订单插入。
- 订单号查询。
- 订单搜索。
字段名 | 字段类型 | 字段描述 | |
order_id | String | 主键 | 订单号 |
customer_name | 属性列 | 消费者姓名 | |
product_name | 产品名 | ||
product_type | 产品类型 | ||
order_time | 下单时间 | ||
pay_time | 支付时间 |
订单表
开发步骤
初始化连接
Tablestore支持Http/Https协议访问服务端,使用Python SDK发起请求前,您需要初始化一个OTSClinet实例,初始化需要获取到服务地址(endpoint)、实例名(instanceName)、密钥(accessKeyId、accessSecret)等信息。代码如下
if __name__ == '__main__':
client = OTSClient("https://order-instance.cn-beijing.ots.aliyuncs.com",# your endpoint
"",# your accessKeyId
"",# your accessSecret
"order-instance")#your insntace name
#operation_method(client)
创建数据表
示例代码中创建了一张订单数据表order。
# 创建数据表
def createOrderTable(client):
schema_of_primary_key = [('order_id', 'STRING')]
table_meta = TableMeta("order", schema_of_primary_key)
# 设置表数据生命周期为永久,设置表最大版本数为1
table_option = TableOptions(-1, 1)
# 默认设置表预留读写cu为0
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
# 发送创建数据表请求
client.create_table(table_meta, table_option, reserved_throughput)
print ('Table has been created.')
写入数据
示例代码中写入了一条订单数据,订单号order_id为“o1”。样例中模拟了一万条订单数据,这里不作展示。
def putOrder(client):
primary_key = [('order_id', 'o1')]
attribute_columns = [('customer_name', '消十一'),('product_name', 'iphone 6'),('product_type', '手机'),('order_time', '2021-10-25 09:20:01'),('pay_time', '2017-10-25 10:00:01')]
table_name = 'order'
row = Row(primary_key, attribute_columns)
condition = Condition(RowExistenceExpectation.IGNORE)
consumed, return_row = client.put_row(table_name, row, condition)
print (u'Write succeed, consume %s write cu.' % consumed.write)
查询数据
示例代码中查询订单号order_id为“o1”的记录
def getOrder(client):
primary_key = [('order_id', 'o1')]
columns_to_get = ['customer_name','product_name','product_type','order_time','pay_time']
table_name = "order"
consumed, return_row, next_token = client.get_row(table_name, primary_key, columns_to_get, None, 1)
print ('Value of primary key: %s' % return_row.primary_key)
print ('Value of attribute: %s' % return_row.attribute_columns)
创建多元索引
示例代码中创建了一个多元索引order_index。分别设置customer_name字符串类型、order_time字符串类型、pay_time字符串类型、product_name分词类型、product_type字符串类型。关于索引字段类型的介绍请参考
多元索引概述。
def createSearchIndex(client):
field_a = FieldSchema('customer_name', FieldType.KEYWORD, index=True, enable_sort_and_agg=True, store=True)
field_b = FieldSchema('order_time', FieldType.KEYWORD, index=True, enable_sort_and_agg=True, store=True)
field_c = FieldSchema('pay_time', FieldType.KEYWORD, index=True, enable_sort_and_agg=True, store=True)
field_d = FieldSchema('product_type', FieldType.TEXT, index=True, store=True, analyzer=AnalyzerType.SINGLEWORD)
field_e = FieldSchema('customer_name', FieldType.KEYWORD, index=True, enable_sort_and_agg=True, store=True)
fields = [field_a, field_b, field_c, field_d, field_e]
index_meta = SearchIndexMeta(fields, None, None)
client.create_search_index("order", "order_index", index_meta)
print ('create search index succeed')
搜索数据
示例代码中查询产品类型为“手机”的订单,并统计了符合条件的行数。
def searchQuery1():
query = TermQuery('product_type', '手机')
search_response = client.search(
"order", "order_index",
SearchQuery(query, limit=100, get_total_count=True),
ColumnsToGet(return_type=ColumnReturnType.ALL)
)
示例代码中搜索产品名包含“iphone”的订单,并统计了符合条件的行数。
def searchQuery2():
query = MatchQuery('product_name', 'iphone')
search_response = client.search(
"order", "order_index",
SearchQuery(query, limit=100, get_total_count=True),
ColumnsToGet(return_type=ColumnReturnType.ALL)
)
示例代码中查询了消费者姓名为“消十一”并且下单时间在“2021-10-24 00:00:00”之间的订单。并统计了行数。
def searchQuery3():
bool_query = BoolQuery(
must_queries=[
TermQuery('customer_name', '消十一'),
RangeQuery('order_time', range_to='2021-10-24 00:00:00', include_upper=False)
]
)
search_response = client.search(
"order", "order_index",
SearchQuery(
bool_query,
None,
limit=10,
get_total_count=True),
ColumnsToGet(return_type=ColumnReturnType.ALL)
)
删除多元索引
示例代码中展示了删除订单表order中的order_index多元索引。
def delete_search_index(index_name):
client.delete_search_index("order", "order_index")
删除数据表
示例代码中展示了删除订单表order。删除表之前需确保先删除表中的多元索引。
def delete_table():
client.delete_table("order")
更多关于Tablestore Python SDK的介绍请参考
Tablestore Python SDK