This topic describes how to create a data table by calling the CreateTable operation. When you call the CreateTable operation to create a data table, you must specify schema information and configuration information for the data table. If the data table belongs to a high-performance instance, you can configure the reserved read throughput and the reserved write throughput based on your business requirements. You can create one or more index tables when you create a data table.
Usage notes
After you create a data table, a few seconds are required to load the data table. During this period, all read and write operations on the data table fail. Perform operations on the data table after the data table is loaded.
You must specify the primary key when you create a data table. A primary key consists of one to four primary key columns. Specify a name and data type for each primary key column.
In system design scenarios that require a unique identifier for each object, such as item IDs on e-commerce websites, user IDs on large websites, post IDs in forums, and message IDs in chat tools, you can specify an auto-increment primary key column when you create a data table. For more information, see Configure an auto-increment primary key column.
Prerequisites
An instance is created in the Tablestore console. For more information, see Create instances.
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
API operation
/**
* Create a data table. Specify the number, names, sequence, and types of primary key columns, reserved read and write throughput, time to live (TTL), and Stream options.
* @api
* @param [] $request The request parameters.
* @return [] The response is empty. If the CreateTable operation is successful, no message is returned. An empty array is returned to be consistent with other operations.
* @throws OTSClientException The exception that is returned when a parameter error occurs or when the Tablestore server returns a verification error.
* @throws OTSServerException The exception that is thrown when the Tablestore server returns an error.
*/
public function createTable(array $request);
Parameters
Request information
Request parameters
Parameter | Description |
table_meta | The schema information about the data table. The schema information includes the following parameters:
|
table_options | The configuration information about the data table. For more information, see Data versions and TTL. The configuration information includes the following parameters:
|
reserved_throughput | The reserved read throughput and reserved write throughput of the data table. You can set the reserved read throughput and reserved write throughput only to 0 for data tables in capacity instances. Reserved throughput does not apply to capacity instances. The default value 0 specifies that you are charged for all throughput on a pay-as-you-go basis. Unit: capacity unit (CU).
|
stream_spec | (Optional) The Stream configurations.
|
index_metas | The schema information about the index table. Each index_meta includes the following parameters:
|
Request syntax
$result = $client->createTable([
'table_meta' => [ // Specify the schema information about the data table. This parameter is required.
'table_name' => '<string>',
'primary_key_schema' => [
['<string>', <PrimaryKeyType>],
['<string>', <PrimaryKeyType>],
['<string>', <PrimaryKeyType>, <PrimaryKeyOption>]
]
],
'reserved_throughput' => [ // Specify the reserved read and write throughput of the data table. This parameter is required.
'capacity_unit' => [
'read' => <integer>,
'write' => <integer>
]
],
'table_options' => [ // Specify the configuration information about the data table. This parameter is required.
'time_to_live' => <integer>,
'max_versions' => <integer>,
'deviation_cell_version_in_sec' => <integer>
],
'stream_spec' => [
'enable_stream' => true || false,
'expiration_time' => <integer>
]
]);
Response information
Response parameters
The response is empty. If an error occurs, the system returns an exception.
Response syntax
[]
Examples
Create a data table without creating an index table
The following sample code provides an example on how to create a data table without creating an index table for the data table. In this example, the data table consists of the following primary key columns: the PK0 primary key column of the Integer type, the PK1 primary key column of the String type, and the PK2 primary key column of the Binary type. Up to two versions of data can be retained for each attribute column in the data table and data in the data table never expires.
$result = $client->createTable([
'table_meta' => [
// Specify the name of the data table.
'table_name' => 'SampleTable',
// Add primary key columns to the data table.
'primary_key_schema' => [
['PK0', PrimaryKeyTypeConst::CONST_INTEGER],
['PK1', PrimaryKeyTypeConst::CONST_STRING],
['PK2', PrimaryKeyTypeConst::CONST_BINARY]
]
],
'reserved_throughput' => [
// Specify the reserved read throughput and reserved write throughput. You can set the reserved read throughput or reserved write throughput only to 0 for data tables in a capacity instance. You can set the reserved read throughput or reserved write throughput to a value other than 0 for data tables in a high-performance instance.
'capacity_unit' => [
'read' => 0,
'write' => 0
]
],
'table_options' => [
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the time_to_live parameter to -1 for a data table for which you want to create an index table.
'time_to_live' => -1,
// Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. You must set the max_versions parameter to 1 for a data table for which you want to create an index table.
'max_versions' => 2,
// Configure the deviation_cell_version_in_sec parameter, which specifies the maximum difference between the current system time and the time when data is written.
'deviation_cell_version_in_sec' => 86400
],
'stream_spec' => [
// Enable Stream.
'enable_stream' => true,
// Specify the expiration time of Stream data.
'expiration_time' => 24
]
]);
Create a data table and global secondary indexes
The following sample code provides an example on how to create a data table and two global secondary indexes for the data table at the same time. In this example, the data table consists of the following primary key columns: the PK0 primary key column of the Integer type and the PK1 primary key column of the String type. The following predefined columns are specified for the data table: the col1 column of the String type and the col2 column of the Integer type. Up to two versions of data can be retained for each attribute column in the data table and data in the data table never expires. The global secondary index named indexName1 consists of the col1, PK0, and PK1 primary key columns and the col2 attribute column. The global secondary index named indexName2 consists of the PK1 and PK0 primary key columns and the col1 and col2 attribute columns.
$request = array (
'table_meta' => array (
// Specify the name of the data table.
'table_name' => 'SampleTable',
// Add primary key columns to the data table.
'primary_key_schema' => array (
array('PK0', PrimaryKeyTypeConst::CONST_INTEGER),
array('PK1', PrimaryKeyTypeConst::CONST_STRING)
),
// Add attribute columns to the data table.
'defined_column' => array(
array('col1', DefinedColumnTypeConst::DCT_STRING),
array('col2', DefinedColumnTypeConst::DCT_INTEGER)
)
),
'reserved_throughput' => array (
// Specify the reserved read throughput and reserved write throughput. You can set the reserved read throughput or reserved write throughput only to 0 for data tables in a capacity instance. You can set the reserved read throughput or reserved write throughput to a value other than 0 for data tables in a high-performance instance.
'capacity_unit' => array (
'read' => 0,
'write' => 0
)
),
'table_options' => [
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the time_to_live parameter to -1 for a data table for which you want to create an index table.
'time_to_live' => -1,
// Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. You must set the max_versions parameter to 1 for a data table for which you want to create an index table.
'max_versions' => 1,
// Configure the deviation_cell_version_in_sec parameter, which specifies the maximum difference between the current system time and the time when data is written.
'deviation_cell_version_in_sec' => 86400
],
'index_metas' => array(
array(
// Specify the name of the index table.
'name' => 'indexName1',
// Specify the index columns of the index table. The index columns are a combination of all primary key columns and a random number of predefined columns of the data table.
'primary_key' => array('col1'),
// Specify the attribute columns of the index table. The attribute columns are a combination of predefined columns of the data table.
'defined_column' => array('col2'),
// Set the index type to global secondary index.
'index_type' => IndexTypeConst::GLOBAL_INDEX,
// Set the index update mode to asynchronous update.
'index_update_mode' => IndexUpdateModeConst::ASYNC_INDEX
),
array(
'name' => 'indexName2',
'primary_key' => array('PK1'),
'defined_column' => array('col1', 'col2')
)
)
);
// Call the method to create the table.
$result = $client->createTable($request);
Create a data table and local secondary indexes
The following sample code provides an example on how to create a data table and two local secondary indexes for the data table at the same time. In this example, the data table consists of the following primary key columns: the PK0 primary key column of the Integer type and the PK1 primary key column of the String type. The following predefined columns are specified for the data table: the col1 column of the String type and the col2 column of the Integer type. Up to two versions of data can be retained for each attribute column in the data table and data in the data table never expires. The local secondary index named indexName3 consists of the PK0, col1, and PK2 primary key columns and the col2 attribute column. The local secondary index named indexName4 consists of the PK0, col2, and PK1 primary key columns and the col1 attribute column.
$request = array (
'table_meta' => array (
// Specify the name of the data table.
'table_name' => 'SampleTable',
// Add primary key columns to the data table.
'primary_key_schema' => array (
array('PK0', PrimaryKeyTypeConst::CONST_INTEGER),
array('PK1', PrimaryKeyTypeConst::CONST_STRING)
),
// Add attribute columns to the data table.
'defined_column' => array(
array('col1', DefinedColumnTypeConst::DCT_STRING),
array('col2', DefinedColumnTypeConst::DCT_INTEGER)
)
),
// Specify the reserved read throughput and reserved write throughput. You can set the reserved read throughput or reserved write throughput only to 0 for data tables in a capacity instance. You can set the reserved read throughput or reserved write throughput to a value other than 0 for data tables in a high-performance instance.
'reserved_throughput' => array (
'capacity_unit' => array (
'read' => 0,
'write' => 0
)
),
'table_options' => array(
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the time_to_live parameter to -1 for a data table for which you want to create an index table.
'time_to_live' => -1,
// Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. You must set the max_versions parameter to 1 for a data table for which you want to create an index table.
'max_versions' => 1,
// Configure the deviation_cell_version_in_sec parameter, which specifies the maximum difference between the current system time and the time when data is written.
'deviation_cell_version_in_sec' => 86400
),
'index_metas' => array(
array(
// Specify the name of the index table.
'name' => 'indexName3',
// Specify the index columns of the index table. The index columns are a combination of all primary key columns and a random number of predefined columns of the data table.
'primary_key' => array('PK0', 'col1'),
// Specify the attribute columns of the index table. The attribute columns are a combination of predefined columns of the data table.
'defined_column' => array('col2'),
// Set the index type to local secondary index.
'index_type' => IndexTypeConst::LOCAL_INDEX,
// Set the index update mode to asynchronous update.
'index_update_mode' => IndexUpdateModeConst::SYNC_INDEX
),
array(
'name' => 'indexName4',
'primary_key' => array('PK0', 'col2'),
'defined_column' => array('col1'),
'index_type' => IndexTypeConst::LOCAL_INDEX,
'index_update_mode' => IndexUpdateModeConst::SYNC_INDEX
)
)
);
// Call the method to create the table.
$result = $client->createTable($request);
References
For information about the API operation that you can call to create a data table, see CreateTable.
You can call API operations to read and write data in a table. For more information, see Basic operations on data.
You can update a table to modify the information about the table, such as the TTL and max versions. For more information, see Update data tables.
After you enable the local transaction feature for a data table, you can create a local transaction and perform read and write operations on the data in the local transaction. For more information, see Configure local transaction.
You can query the names of tables to view all existing tables in an instance. For more information, see List the names of tables.
You can query the description of a table to view the configuration information about the table, such as the max versions and TTL. For more information, see Query the description of a table.
You can delete a data table that you no longer require. For more information, see Delete tables.