This topic describes how to create a table by calling the CreateTable operation. You must specify the schema information and configuration information of the table when you call the CreateTable operation. You can set reserved read and write throughput for tables in high performance instances. You can create one or more index tables when you create the base table.
- After a table is created, it takes several seconds to load the table. During this period, the read and write operations performed on the table may fail. Perform operations on a table after the table is loaded.
- You must specify the primary key when you create a table. A primary key can contain one to four primary key columns. Each primary key column has a name and a data type.
""" Description: You can call this operation to create a table based on the specified table schema information. table_meta indicates an instance of the tablestore.metadata.TableMeta class. table_meta specifies the table name and primary key schema. For more information, see the documentation for the TableMeta class. After you create a table, it takes several seconds to load the partitions in the table. You can perform operations on the table only after the partitions are loaded. table_options is an instance of the tablestore.metadata.TableOptions class. table_options contains the time_to_live, max_version, and max_time_deviation parameters. reserved_throughput is an instance of the tablestore.metadata.ReservedThroughput class. reserved_throughput specifies the reserved read throughput and reserved write throughput. secondary_indexes is an array that can contain one or more instances of the tablestore.metadata.SecondaryIndexMeta class. secondary_indexes specifies the global secondary index you want to create. Return value: none. """ def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=):
|table_meta||The schema information of the table, which includes the following items:
|table_options||The configurations of the table. For more information, see Max versions and TTL.
The configurations include the following items:
|reserved_throughput||The reserved read and write throughput of the table.
For capacity instances, the reserved read and write throughput can be set only to 0. Reserved throughput does not apply to these instances.
The default value 0 indicates that all throughput is billed on a pay-as-you-go basis.
|secondary_indexes||The schema information of the index table, which includes the following items:
- Create a base table without creating an index table
The following code provides an example on how to create a table that contains two primary key columns. In this example, time_to_live is set to 31,536,000 (one year), max_version to 3, max_time_deviation to 86,400 (one day), and reserved_throughput to (0,0).
# Create a schema for the primary key columns, including the count, names, and types of primary key columns. # The first primary key column is of the INTEGER type and named pk0. The first primary key column is partition key. # The second primary key column is of the INTEGER type and named pk1. The data type is specified as INTEGER in this example. You can also set the data type to STRING or BINARY. schema_of_primary_key = [('pk0', 'INTEGER'), ('pk1', 'INTEGER')] # Create a tableMeta instance based on the table name and the schema of primary key columns. table_meta = TableMeta('SampleTable', schema_of_primary_key) # Create a TableOptions instance. Set time_to_live to 31,536,000 to automatically delete expired data, max_version to 3, and max_time_deviation to 86,400 (one day). table_options = TableOptions(31536000, 3, 86400) # Set both the reserved read throughput and reserved write throughput to 0. reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) # Call the create_table method of the client. If no exceptions occur, the table is created. If an exception occurs, the table fails to be created. try: ots_client.create_table(table_meta, table_options, reserved_throughput) print "create table succeeded" # If the task fails, an exception is returned for you to handle. except Exception: print "create table failed."
For the detailed sample code, visit CreateTable@GitHub.
- Create an index table when you create the base table
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)