使用CreateTable接口创建数据表时,需要指定数据表的结构信息和配置信息,高性能实例中的数据表还可以根据需要设置预留读/写吞吐量。创建数据表的同时支持创建一个或者多个索引表。
说明
- 创建数据表后需要几秒钟进行加载,在此期间对该数据表的读/写数据操作均会失败。请等待数据表加载完毕后再进行数据操作。
- 创建数据表时必须指定数据表的主键。主键包含1个~4个主键列,每一个主键列都有名称和类型。
前提条件
接口
"""
说明:根据指定表结构信息创建数据表。
``table_meta``是``tablestore.metadata.TableMeta``类的实例,它包含数据表名称和PrimaryKey的schema。
请参见``TableMeta``类的文档。当创建一个数据表后,通常需要等待几秒钟时间使partition load完成,才能进行各种操作。
``table_options``是``tablestore.metadata.TableOptions``类的实例,它包含time_to_live,max_version和max_time_deviation三个参数。
``reserved_throughput``是``tablestore.metadata.ReservedThroughput``类的实例,表示预留读写吞吐量。
``secondary_indexes``是一个数组,可以包含一个或多个``tablestore.metadata.SecondaryIndexMeta``类的实例,表示要创建的全局二级索引。
返回:无。
"""
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[]):
参数
参数 | 说明 |
---|---|
table_meta | 数据表的结构信息,包括如下内容:
|
table_options | 数据表的配置信息。更多信息,请参见数据版本和生命周期。 配置信息包括如下内容:
|
reserved_throughput | 为数据表配置预留读吞吐量或预留写吞吐量。 容量型实例中的数据表的预留读/写吞吐量只能设置为0,不允许预留。 默认值为0,即完全按量计费。 单位为CU。
|
secondary_indexes | 索引表的结构信息,每个SecondaryIndexMeta包括如下内容:
|
示例
- 创建数据表(不带索引)
创建一个有2个主键列,数据保留1年(60*60*24*365=31536000秒),最大版本数3,写入时间戳偏移小于1天(86400秒),预留读写吞吐量为(0, 0)的数据表。
# 创建主键列的schema,包括主键的个数、名称和类型。 # 第一个PK列为整型,名称是pk0,该列同时也是分区键。 # 第二个PK列为整型,名称是pk1。其他可选的类型包括STRING和BINARY,此处使用INTEGER。 schema_of_primary_key = [('pk0', 'INTEGER'), ('pk1', 'INTEGER')] # 通过数据表名称和主键列的schema创建一个tableMeta。 table_meta = TableMeta('SampleTable', schema_of_primary_key) # 创建TableOptions,数据保留31536000秒,超过后自动删除;最大3个版本;写入时指定的版本值和当前时间相差不能超过86400秒(即1天)。 table_options = TableOptions(31536000, 3, 86400) # 设置预留读吞吐量为0,预留写吞吐量为0。 reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) # 调用client的create_table接口,如果没有抛出异常,则说明执行成功。 try: ots_client.create_table(table_meta, table_options, reserved_throughput) print "create table succeeded" # 如果抛出异常,则说明执行失败,处理异常。 except Exception: print "create table failed."
详细代码请参见CreateTable@GitHub。
- 创建数据表(带索引且索引类型为全局二级索引)
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'STRING')] defined_columns = [('i', 'INTEGER'), ('bool', 'BOOLEAN'), ('d', 'DOUBLE'), ('s', 'STRING'), ('b', 'BINARY')] table_meta = TableMeta(table_name, schema_of_primary_key, defined_columns) table_option = TableOptions(-1, 1) reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) secondary_indexes = [ SecondaryIndexMeta('index1', ['i', 's'], ['bool', 'b', 'd']), ] client.create_table(table_meta, table_option, reserved_throughput, secondary_indexes)
- 创建数据表(带索引且索引类型为本地二级索引)
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'STRING')] defined_columns = [('i', 'INTEGER'), ('bool', 'BOOLEAN'), ('d', 'DOUBLE'), ('s', 'STRING'), ('b', 'BINARY')] table_meta = TableMeta(table_name, schema_of_primary_key, defined_columns) table_option = TableOptions(-1, 1) reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) secondary_indexes = [ SecondaryIndexMeta('index1', ['gid', 's'], ['bool', 'b', 'd'],index_type= SecondaryIndexType.LOCAL_INDEX), ] client.create_table(table_meta, table_option, reserved_throughput, secondary_indexes)