Create a data table
Updated at:
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.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Method description
public CreateTableResponse CreateTable(CreateTableRequest request)
Asynchronous method:
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request)
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;
References
Is this page helpful?