This topic describes how to create a data table in Tablestore by using the .NET SDK.
Usage notes
After you create a data table, wait until the data table is loaded before you perform operations on data. Otherwise, the operations will fail. This process typically takes several seconds.
Method description
public CreateTableResponse CreateTable(CreateTableRequest request)
Asynchronous method:
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request)
CreateTableRequest parameters
TableMeta (required) TableMeta: The table schema information, which includes the following parameters.
Parameter | Type | Description |
TableName (required) | string | The name of the data table. |
PrimaryKeySchema (required) | PrimaryKeySchema | The information about the primary key. You can configure 1 to 4 primary key columns, which are sorted in ascending order by default. The first primary key column serves as the partition key. The data types of primary key columns include STRING, INTEGER, and BINARY. You can specify a primary key column of the INTEGER type that is not the partition key as an auto-increment primary key column.
|
DefinedColumnSchema (optional) | DefinedColumnSchema | The information about predefined columns. Predefined columns are attribute columns that are predefined. You can use predefined columns to create secondary indexes and search indexes. The data types of predefined columns include STRING, INTEGER, BINARY, DOUBLE, and BOOLEAN.
|
TableOptions (optional) TableOptions: The configuration information of the table, which includes the following parameters.
Parameter | Type | Description |
TimeToLive (optional) | int | The time to live (TTL) of data, in seconds. The default value is -1. When this parameter is set to -1, the data never expires. Otherwise, the minimum value is 86400 (one day). Data whose retention period exceeds the TTL is automatically deleted. If you want to use search indexes or secondary indexes, you must set this parameter to -1 or set the AllowUpdate parameter to false.
|
MaxVersions (optional) | int | The maximum number of versions. The default value is 1. |
DeviationCellVersionInSec (optional) | long | The maximum version drift, in seconds. The default value is 86400 (one day). The difference between the current system time and the timestamp of written data must be within the maximum version offset. Otherwise, the write operation fails. The valid version range for attribute column data is [max(Data written time - Maximum version offset, Data written time - TTL), Data written time + Maximum version offset).
|
AllowUpdate (optional) | bool | Specifies whether to allow updates. The default value is true. |
IndexMetas (optional) List<IndexMeta>: The list of secondary indexes. Each index includes the following parameters.
Parameter | Type | Description |
IndexName (required) | string | The name of the index. |
PrimaryKey (required) | List<string> | The primary key columns of the index. |
DefinedColumns (optional) | List<string> | The predefined columns of the index. |
IndexType (optional) | IndexType | The type of the index. The fixed value is IT_GLOBAL_INDEX, which indicates a global secondary index. |
IndexUpdateModel (optional) | IndexUpdateMode | The update mode of the index. The fixed value is IUM_ASYNC_INDEX, which indicates asynchronous update. |
StreamSpecification (optional) StreamSpecification: The Stream configuration information, which includes the following parameters.
Parameter | Type | Description |
EnableStream (required) | bool | Specifies whether to enable Stream. The default value is false. |
ExpirationTime (optional) | int | The expiration time of the Stream, which indicates the retention period of incremental logs. The unit is hours. The maximum value is 168 (seven days). |
ReservedThroughput (required) CapacityUnit: The reserved read and write throughput, in capacity units (CUs). You can set this parameter to a non-zero value and the setting takes effect only for data tables in high-performance instances in CU mode.
Sample code
The following sample code creates a table named test_table that contains one primary key column of the String type.
try
{
// At least one primary key column is required to create a data table.
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
{
{ "id", ColumnValueType.String }
};
// Construct the schema information of the data table.
TableMeta tableMeta = new TableMeta("test_table", primaryKeySchema);
// You must set the reserved read and write throughput when you create a data table (you can set this parameter to a non-zero value and the setting takes effect only for data tables in high-performance instances in CU mode).
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
// Construct the request and send it.
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
client.CreateTable(request);
Console.WriteLine("Create table succeeded.");
}
catch (Exception ex)
{
Console.WriteLine($"Create table failed, exception:{ex.Message}");
}
You can also refer to the following sample code to configure additional settings when you create a data table.
Add predefined columns
DefinedColumnSchema definedColumnSchema = new DefinedColumnSchema
{
{ "name" , DefinedColumnType.STRING }
};
tableMeta.DefinedColumnSchema = definedColumnSchema;
Specify the maximum version offset
TableOptions tableOptions = new TableOptions();
tableOptions.DeviationCellVersionInSec = 86400;
request.TableOptions = tableOptions;
Specify whether to allow updates
TableOptions tableOptions = new TableOptions();
tableOptions.AllowUpdate = false;
request.TableOptions = tableOptions;
Add a secondary index
// Construct a list of secondary indexes.
List<IndexMeta> indexMetas = new List<IndexMeta>();
// Construct a secondary index.
IndexMeta indexMeta = new IndexMeta("test_table_index");
// Specify the primary key of the index.
indexMeta.PrimaryKey = new List<string>() { "id", "name" };
// Specify the index type.
indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX;
// Specify the index update mode.
indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX;
// Add the secondary index.
indexMetas.Add(indexMeta);
// Construct the request.
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas);
Specify Stream information
StreamSpecification streamSpecification = new StreamSpecification(true);
streamSpecification.ExpirationTime = 168;
request.StreamSpecification = streamSpecification;