You can call the CreateTable operation to create a table based on the specified table schema information.
- After the table is created, it takes about several seconds to load the table. During this period, the read and write operations on the table may fail. You can use applications to perform operations on the table data only after the table is loaded.
- When you create a table in Tablestore, you must specify the primary key of the table. A primary key contains 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 about 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. Response: null. def create_table(self, table_meta, reserved_throughput):
The following code provides an example on how to create a table. In this example, a table with two primary key columns is created. time_to_live is set to 31,536,000 seconds (one year), max_version to 3, max_time_deviation to 86,400 seconds (one day), and reserved_throughput to (0,0).
# Create a schema for the primary key columns, including the quantity, names, and data types of primary keys. # The first primary key column (partition key) is named pk0 and of the INTEGER type. # The second primary key column is named pk1 and of the INTEGER type. You can also set the data type to STRING or BINARY. INTEGER is specified in this example. schema_of_primary_key = [('pk0', 'INTEGER'), ('pk1', 'INTEGER')] # Create a table_meta instance based on the table name and the schema of primary key columns. table_meta = TableMeta('SampleTable', schema_of_primary_key) # Create a table_options instance. Set time_to_live to 31,536,000 seconds (data is automatically cleared when it expires), max_version to 3, and max_time_deviation to 86,400 seconds (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 create. try: ots_client.create_table(table_meta, table_options, reserved_throughput) print "create table succeeded" # Handle any exception that occurs. except Exception: print "create table failed."
You can obtain the complete sample code at CreateTable@GitHub.