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 based on the specified table schema.
*/
createTable(params, callback)
Parameters
The following table describes the parameters that you can configure when you call the CreateTable operation to create a data table.
Parameter | description |
tableMeta | The schema information about the data table. The schema information includes the following parameters:
|
tableOptions | Optional. The configuration information about the data table. For more information, see Data versions and TTL. The configuration information includes the following parameters:
|
reservedThroughput | 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).
|
indexMetas | The schema information about the index tables. Each indexMeta includes the following parameters:
|
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 two primary key columns and 0 is specified as the reserved read throughput and reserved write throughput for the data table.
var client = require('./client');
var params = {
tableMeta: {
tableName: 'sampleTable',
primaryKey: [
{
name: 'gid',
type: 'INTEGER'
},
{
name: 'uid',
type: 'INTEGER'
}
]
},
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
tableOptions: {
timeToLive: -1, // Specify the validity period of data in seconds. A value of -1 specifies that the data never expires. If you want to set the validity period to one year, set the timeToLive parameter to 31536000. The value is calculated by using the following formula: 365 × 24 × 3,600.
maxVersions: 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.
}
};
client.createTable(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
Create a data table and a global secondary index
The following sample code provides an example on how to create a data table and a global secondary index for the data table at the same time. In this example, the data table consists of the pk1 primary key column of the Integer type and the pk2 primary key column of the Integer type. The col1 column of the Integer type and the col2 column of the Integer type are specified as predefined columns of the data table. Only the latest version of data is retained for each attribute column in the data table and data in the data table never expires. The global secondary index named sdkGlobalIndex2 consists of the col1, pk1, and pk2 primary key columns and the col2 attribute column.
var client = require('./client');
var TableStore = require('../index.js');
var params = {
tableMeta: {
tableName: 'sdkGlobalTest',
primaryKey: [
{
name: 'pk1',
type: TableStore.PrimaryKeyType.INTEGER
},
{
name: 'pk2',
type: TableStore.PrimaryKeyType.INTEGER
}
],
definedColumn: [
{
"name": "col1",
"type": TableStore.DefinedColumnType.DCT_INTEGER
},
{
"name": "col2",
"type": TableStore.DefinedColumnType.DCT_INTEGER
}
],
},
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
tableOptions: {
timeToLive: -1, // Specify the validity period of data in seconds. A value of -1 specifies that the data never expires. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
maxVersions: 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 maxVersions parameter to 1 for a data table for which you want to create an index table.
},
streamSpecification: {
enableStream: false, // The Stream feature cannot be enabled for secondary indexes.
},
indexMetas: [
{
name: "sdkGlobalIndex1",
primaryKey: ["pk2"],
definedColumn: ["col1", "col2"]
},
{
name: "sdkGlobalIndex2",
primaryKey: ["col1"],
definedColumn: ["col2"]
}
]
};
client.createTable(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
Create a data table and a local secondary index
The following sample code provides an example on how to create a data table and a local secondary index for the data table at the same time. In this example, the data table consists of the pk1 primary key column of the Integer type and the pk2 primary key column of the Integer type. The col1 column of the Integer type and the col2 column of the Integer type are specified as predefined columns of the data table. Only the latest version of data is retained for each attribute column in the data table and data in the data table never expires. The local secondary index named sdklocalIndex1 consists of the pk1, col1, and pk2 primary key columns and the col2 attribute column.
var client = require('./client');
var TableStore = require('../index.js');
var params = {
tableMeta: {
tableName: 'sdkLocalTest',
primaryKey: [
{
name: 'pk1',
type: TableStore.PrimaryKeyType.INTEGER
},
{
name: 'pk2',
type: TableStore.PrimaryKeyType.INTEGER
}
],
definedColumn: [
{
"name": "col1",
"type": TableStore.DefinedColumnType.DCT_INTEGER
},
{
"name": "col2",
"type": TableStore.DefinedColumnType.DCT_INTEGER
}
],
},
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
tableOptions: {
timeToLive: -1, // Specify the validity period of data in seconds. A value of -1 specifies that the data never expires. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
maxVersions: 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 maxVersions parameter to 1 for a data table for which you want to create an index table.
},
streamSpecification: {
enableStream: false, // The Stream feature cannot be enabled for secondary indexes.
},
indexMetas: [
{
name: "sdklocalIndex1",
primaryKey: ["pk1","col1"], // Add primary key columns to the index table. The first primary key column of the index table must be the same as the first primary key column of the data table.
definedColumn: ["col2"],
indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX, // Set the indexUpdateMode parameter to IUM_SYNC_INDEX, which specifies the synchronous update mode. If you set the indexType parameter to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX.
indexType: TableStore.IndexType.IT_LOCAL_INDEX, // Set the indexType parameter to IT_LOCAL_INDEX, which specifies local secondary index.
},
{
name: "sdklocalIndex2",
primaryKey: ["pk1","col2"],
definedColumn: ["col1"],
indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX, // Set the indexUpdateMode parameter to IUM_SYNC_INDEX, which specifies the synchronous update mode. If you set the indexType parameter to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX.
indexType: TableStore.IndexType.IT_LOCAL_INDEX, // Set the indexType parameter to IT_LOCAL_INDEX, which specifies local secondary index.
}
]
};
client.createTable(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
References
For information about the API operation that you can call to create a data table, see CreateTable. For the detailed sample code, visit CreateTable@GitHub.
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 time to live (TTL) and max versions. For more information, see Update a table.
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 Local transaction.
You can query the names of tables to view all existing tables in an instance. For more information, see Query 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.