Create Table
TableInfo createTable(String tableName, TableSpec tableSpec)
Function
Create a table with the specified table structure and metadata under the developer account
Method parameters
tableName : String : required
The name of the newly created table cannot be a duplicate of the existing table under the account,
Can only be composed of letters (case-sensitive), numbers, underscores and links
tableSpec : TableSpec : required
The new table's schema and meta data definition, including both parts of schema and meta
The schema part includes
1.entityGroupSpec : EntityGroupSpec : optional
entityGroupSpec includes List< KeySpec > attributes, used to specify attributes as an entity group key and the encoding of each attribute (asc/desc)
enableHash specifies whether to enable hash distribution for entity group keys
2.primaryIndex : List< KeySpec > : required
Specifies the attributes as the primary key, and the encoding of each attribute (asc/desc)
3.attributes : Map< String, DataType > : required
The mapping of attribute names to attribute data types defines the table attribute names and the data types of these attributes, and must include the attributes defined in the entityGroupSpec and primaryIndex defined previously.
4.secondaryIndexes : Map< String, LocalSecondaryIndexSpec > : optional
Multiple secondary indexes can be created on the table. This parameter defines the mapping of LocalSecondaryIndexSpec from the secondary index name to the secondary index, and defines the secondary index of the table
Among which is LocalSecondaryIndexSpec, which contains the following fields:
- indexSchema: List< KeySpec > specifies which attributes are to be indexed, and the encoding method (asc/desc) when the key is recorded as an index
- consistencyMode: SecondaryIndexConsistencyMode defines the index type, LAZY, EAGER, or IMMUTABLE
- Projections: List
. This is a list of property names that defines which properties of the master record are included in the index record. This can only be set if the index type is EAGER or IMMUTABLE
5.streams : Map< String, StreamSpec > : optional
Multiple Streams can be created on the table. This parameter defines the StreamSpec mapping from the topic name to the Stream, and defines the Stream collection of the table
StreamSpec contains the following fields:
- viewType: StreamViewType represents the Stream type, including RECORD_IMAGE and MUTATE_LOG
- Attributes: List< String >, needs a list of attribute names of track
- enableStream: boolean, enable switch
6.ttl : int : optional
The survival time of the table data, in seconds, that is, from the start of writing, after which the data will be automatically deleted; if the default is -1, it will never be deleted
7.preSplits : int : optional
The number of initial fragmentation of the table only supports the Entity Group to open the hash distribution table, and only works when the table is being created; the range is [1,256]
meta section
1.quota : TableQuota : required
Specifies table space size quota
2.throughput : ProvisionThroughput : required
Specifies read and write quotas of the table
3.appAcl : Map< String, List< CannedAcl > > : optional
Defines the table's access permissions for the app under this account, refer to Permissions model
4.description : String : optional
Brief description of the table
- exceededThroughput : ProvisionThroughput : required
The maximum read/write quota of the primary cluster, that is, the maximum throughput that the system may reach when it is idle, and the setting is larger than throughput, which allows for hypercapability
- slaveThroughput : ProvisionThroughput : required
Default cluster read/write quotas
- exceededSlaveThroughput : ProvisionThroughput : required
The maximum read/write quota of the standby cluster, that is, the maximum throughput that the system may reach when it is idle, and the setting is larger than slaveThroughput, which allows for hypercapability
Method return value
tableInfo : TableInfo
1.name : String
Name of created table
2.spec : TableSpec
The tableSpec of the created table, compared with the tableSpec parameter passed in, the meta structure of the value has more tableId and developerId
3.state : TableState
Includes the creation time of the table, the latest modification time, the time of the last statistical table space and the number of rows, the table space size, and the number of rows of the table
Exception error code
INTERNAL_ERROR(1): Server exception
ACCESS_DENIED(4): No permission to create table
VALIDATION_FAILED(5): Parameter error
SIZE_EXCEED(6): The table number, space quota or read/write quota exceeds the user's total quota
RESOURCE_ALREADY_EXISTS(10): A table with the same name already exists
RESOURCE_UNAVAILABLE(11) : There are other DDL operations under this account. Does not allow DDL operations at the same time
Limitation
Only developers can create tables
The secondary index is only supported if the entity group key is set
preSplits can be set only if the entity group key is set and hash is on
Example
With the Example table, the sample code for creating table is provided below
$tableName = "php-note";
// create table
$tableSpec = new TableSpec(array(
'schema' => new TableSchema(array(
'entityGroup' => new EntityGroupSpec(array(
'attributes' => array(new KeySpec(array('attribute' => 'userId'))),
'enableHash' => true, // hash distribution
)),
// Creation time order
'primaryIndex' => array(
new KeySpec(array('attribute' => 'noteId', 'asc' => false)),
),
'secondaryIndexes' => array(
// Default display order, sorted by last modify time
'mtime' => new LocalSecondaryIndexSpec(array(
'indexSchema' => array(
new KeySpec(array('attribute' => 'mtime', 'asc' => false)),
),
'projections' => array('title', 'noteId'),
'consistencyMode' => SecondaryIndexConsistencyMode::EAGER,
)),
// Search by category
'cat' => new LocalSecondaryIndexSpec(array(
'indexSchema' => array(
new KeySpec(array('attribute' => 'category')),
),
'consistencyMode' => SecondaryIndexConsistencyMode::LAZY,
)),
),
'attributes' => array(
'userId' => DataType::STRING,
'noteId' => DataType::INT64,
'title' => DataType::STRING,
'content' => DataType::STRING,
'mtime' => DataType::INT64,
'category' => DataType::STRING,
),
)),
'metadata' => new TableMetadata(array(
'quota' => new TableQuota(array('size' => 100 * 1024 * 1024)),
'throughput' => new ProvisionThroughput(array(
'readCapacity' => 200,
'writeCapacity' => 200
))
))
));
$adminClient->createTable($tableName, $tableSpec);