This topic describes how to create a table by calling the CreateTable operation. You must specify the schema information and configuration information of the table when you call the CreateTable operation. You can set reserved read and write throughput for tables in high performance instances. You can create one or more index tables when you create the base table.
- After a table is created, it takes several seconds to load the table. During this period, the read and write operations performed on the table may fail. Perform operations on a table after the table is loaded.
- You must specify the primary key when you create a table. A primary key can contain one to four primary key columns. Each primary key column has a name and a data type.
Prerequisites
- An instance is created in the console. For more information, see Create instances.
- OTSClient is initialized. For more information, see Initialization.
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 parameter</param>
/// <returns>Information returned by CreateTable. The result is null.
/// </returns>
public CreateTableResponse CreateTable(CreateTableRequest request);
/// <summary>
/// Asynchronous mode of CreateTable.
/// </summary>
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request);
Parameters
Parameter | Description |
---|---|
tableMeta | The schema information of the table, which includes the following items:
|
tableOptions | The configurations of the table. For more information, see Max versions and TTL.
The configurations include the following items:
|
reservedThroughput | The reserved read and write throughput of the table.
For capacity instances, the reserved read and write throughput can be set only to 0. Reserved throughput does not apply to these instances. The default value 0 indicates that all throughput is billed on a pay-as-you-go basis. Unit: CU.
|
indexMetas | The schema information of the index tables. Each indexMeta includes the following
items:
|
Examples
- Create a base table without creating an index table
The following code provides an example on how to create a table that has two primary key columns and a reserved read and write throughput of (0, 0):
//Create a schema for the primary key columns, which includes the count, names, and types of primary key columns. //The first primary key column is named pk0 and requires an INTEGER value. The first primary key column is also the partition key. //The second primary key column is named pk1 and requires an INTEGER value. var primaryKeySchema = new PrimaryKeySchema(); primaryKeySchema.Add("pk0", ColumnValueType.Integer); primaryKeySchema.Add("pk1", ColumnValueType.String); //Create a tableMeta class based on the table name and the schema for 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 the CreateTableRequest object. var request = new CreateTableRequest(tableMeta, reservedThroughput); // Call the CreateTable operation of the client. If the table is created, no exception is returned. Otherwise, an exception is returned. otsClient.CreateTable(request); Console.WriteLine("Create table succeeded.") ; } # If the task fails, an exception is returned for you to handle. catch (Exception ex) { Console.WriteLine("Create table failed, exception:{0}", ex.Message); }
For the detailed sample code, visit CreateTable@GitHub.
- Create an index table when you create the base table
public static void CreateTableWithGlobalIndex() { // Create a table that contains two primary key columns Pk1 and Pk2 and the predefined columns Col1 and col2. //Create a global secondary index. Specify Col1 as the first primary key column of the index table. 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); }