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 unique IDs, 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 console. For more information, see Create instances.
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
API operations
/// <summary>
/// Create a table based on table information including the table name, primary key schema, and reserved read and write throughput.
/// </summary>
/// <param name="request">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 CreateTable.
/// </summary>
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request);
Parameters
Parameter | Description |
tableMeta | The schema information about the data table. The schema information contains the following parameters:
|
tableOptions | 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 table. Each indexMeta contains the following parameters:
|
Examples
Create a data table without creating an index table
The following sample code shows how to create a data table that has two primary key columns and reserved read and write throughput of (0, 0).
// Create a schema for the primary key columns, including the number, names, and data types of primary key columns.
// The first primary key column is named pk0 and is of the INTEGER type. The first primary key column is also 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 object 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 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, the operation fails. Handle the exception.
catch (Exception ex)
{
Console.WriteLine("Create table failed, exception:{0}", ex.Message);
}
Configure a global secondary index when you create a data table
The following sample code shows how to create a data table and configure a global secondary index for the data table at the same time. The data table contains two primary key columns: the Pk1 column of the STRING type and the Pk2 column of the STRING type. The data table also contains two predefined columns: the Col1 column of the STRING type and the Col2 column of the STRING type. The primary key columns of the global secondary index are the Col1, Pk1, and Pk2 columns. The attribute column of the global secondary index is the Col2 column.
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 one primary key column of the index table to Col1. The name of the primary key column is Pk0.
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.
You can call API operations to read and write data in a data table. For more information, see Basic operations on data.
You can update a table to modify the information about the table, such as the TTL and max versions. For more information, see UpdateTable.
You can query the names of tables to view all existing tables in an instance. For more information, see List 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 a data table.