This topic describes how to use Tablestore SDK for .NET to create a data table by using parameters and sample code. When you create a data table, you must specify the schema information and configuration information about the data table. You can specify the reserved read and write throughput for a data table in a high-performance instance.
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.
In scenarios that require an auto-increment primary key column, such as item IDs on e-commerce websites and post IDs in forums, 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 an instance.
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
API operation
/// <summary>
/// Create a data table based on the specified table schema.
/// </summary>
/// <param name="request">The request parameters</param>
/// <returns>The information returned by the CreateTable operation. The result is null. </returns>
public CreateTableResponse CreateTable(CreateTableRequest request);
/// <summary>
/// The asynchronous mode of the CreateTable operation. The parameters and calling method are the same as those of the CreateTable operation.
/// </summary>
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request);
Parameters
The following table describes the parameters that are included in request
.
Parameter | Description |
tableMeta (required) | The schema information about the data table. You can configure the following parameters to specify the schema information:
|
tableOptions (optional) | The configuration information about the data table. You can configure the following parameters to specify the configuration information:
|
indexMetas (optional) | The list of indexes. You can configure the following parameters for each index:
Note In Tablestore, secondary indexes are classified into global secondary indexes and local secondary indexes. Tablestore SDK for .NET supports only global secondary indexes. |
reservedThroughput (required) | The reserved read and write throughput. Unit: capacity unit (CU). Default value: 0. Important You can set the reserved read or write throughput to a value other than 0 only for a data table in a high-performance instance. |
streamSpecification (optional) | The Stream configuration information about the data table. You can configure the following parameters to specify the Stream configuration information:
|
Examples
Create a data table
The following sample code provides an example on how to create a data table:
// Create a schema for the primary key columns, including the number, names, and data types of the primary key columns.
// The first primary key column is named pk0 and is of the Integer type. The first primary key column is the partition key.
// The second primary key column is named pk1 and is of the String type.
var primaryKeySchema = new PrimaryKeySchema();
primaryKeySchema.Add("pk0", ColumnValueType.Integer);
primaryKeySchema.Add("pk1", ColumnValueType.String);
// Create a tableMeta instance based on the table name and the schema of the primary key columns.
var tableMeta = new TableMeta("SampleTable", primaryKeySchema);
// Set the reserved read throughput and the reserved write throughput to 0.
var reservedThroughput = new CapacityUnit(0, 0);
try
{
// Construct a CreateTableRequest object.
var request = new CreateTableRequest(tableMeta, reservedThroughput);
// Call the CreateTable operation of the client. If no exception is returned, a table is created.
otsClient.CreateTable(request);
Console.WriteLine("Create table succeeded.");
}
// If an exception is returned, no table is created and you must handle the exception.
catch (Exception ex)
{
Console.WriteLine("Create table failed, exception:{0}", ex.Message);
}
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:
public static void CreateTableWithGlobalIndex()
{
// Create a data table that contains two primary key columns Pk1 and Pk2 and two predefined columns Col1 and Col2.
// Create an index table and set the primary key column to Col1 and the predefined column to Col2.
OTSClient otsClient = Config.GetClient();
Console.WriteLine("Start create table with globalIndex...");
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
{
{ Pk1, ColumnValueType.String },
{ Pk2, ColumnValueType.String }
};
TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);
tableMeta.DefinedColumnSchema = new DefinedColumnSchema {
{ Col1, DefinedColumnType.STRING},
{ Col2, DefinedColumnType.STRING}
};
IndexMeta indexMeta = new IndexMeta(IndexName);
indexMeta.PrimaryKey = new List<string>() { Col1 };
indexMeta.DefinedColumns = new List<string>() { Col2 };
//indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX;
//indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX;
List<IndexMeta> indexMetas = new List<IndexMeta>() { };
indexMetas.Add(indexMeta);
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas);
otsClient.CreateTable(request);
Console.WriteLine("Table is created: " + TableName);
}
References
For information about the API operation that you can call to create a data table, see CreateTable.
For information about data versions and time to live (TTL), secondary indexes, and reserved read and write throughput, see Data versions and TTL, Overview, and What is reserved read and write throughput?.
After you create a data table, you can perform the following operations:
Operations on the table. For more information, see Operations on tables.
Operations on data. For more information, see Basic operations on data.