This topic describes how to create a Tablestore data table using PHP SDK.
Usage notes
After you create a data table, wait until the table is loaded before performing operations on data. Otherwise, the operations will fail. This process typically takes several seconds.
Prerequisites
Method description
public function createTable(array $request)
Sample code
Create a data table
The following sample code creates a test_table table that contains one String type primary key.
$request = array (
// Table structure information
'table_meta' => array (
'table_name' => 'test_table',
// At least one primary key is required to create a data table
'primary_key_schema' => array (
array('id', PrimaryKeyTypeConst::CONST_STRING)
),
// (Optional) Add predefined columns
'defined_column' => array (
array('name', DefinedColumnTypeConst::DCT_STRING)
)
),
// (Optional) Table configuration information
'table_options' => array (
// (Optional) Data lifecycle, -1 indicates that data never expires
'time_to_live' => -1,
// (Optional) Maximum number of versions
'max_versions' => 1,
// (Optional) Maximum version offset
'deviation_cell_version_in_sec' => 86400,
// (Optional) Whether to allow updates
'allow_update' => true
),
// (Optional) Set Stream information
'stream_spec' => array (
'enable_stream' => true,
'expiration_time' => 168
),
// Reserved read and write throughput must be set when creating a data table (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
'reserved_throughput' => array (
'capacity_unit' => array (
'read' => 0,
'write' => 0
)
)
);
try{
$client->createTable( $request );
echo "Create table succeeded.";
} catch (Exception $e) {
echo "Create table failed.";
}
Create a data table with secondary indexes
The following sample code creates a data table with secondary indexes.
$request = array (
// Table structure information
'table_meta' => array (
'table_name' => 'test_table',
// Primary key
'primary_key_schema' => array (
array('id', PrimaryKeyTypeConst::CONST_STRING)
),
// Predefined columns
'defined_column' => array (
array('name', DefinedColumnTypeConst::DCT_STRING)
)
),
// Reserved read and write throughput (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
'reserved_throughput' => array (
'capacity_unit' => array (
'read' => 0,
'write' => 0
)
),
// Secondary index information
'index_metas' => array(
array(
'name' => 'test_table_index',
'primary_key' => array('id'),
'defined_column' => array('name'),
'index_type' => IndexTypeConst::LOCAL_INDEX,
'index_update_mode' => IndexUpdateModeConst::SYNC_INDEX
)
)
);
try{
$client->createTable( $request );
echo "Create table succeeded.";
} catch (Exception $e) {
echo "Create table failed.";
}