All Products
Search
Document Center

Tablestore:Create a data table

Last Updated:Nov 08, 2024

This topic describes how to create a data table by calling the CreateTable operation. When you call the CreateTable operation, you must specify the schema information and configuration information about the data table. You can also configure the reserved read throughput and reserved write throughout based on your business requirements if the data table belongs to a high-performance instance. You can create one or more index tables when you create a data table.

Usage notes

  • It takes several seconds to load a data table after the data table is created. 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 a unique identifier for each object, 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

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

Configure the parameters in the code based on the parameters described in the following table and the "Request syntax" section of the CreateTable topic.

Parameter

Required

Description

tableMeta

Yes

The schema information about the data table. The schema information includes the following parameters:

Note

You do not need to specify the schema for attribute columns. Different rows in a Tablestore table can have different attribute columns. You can specify the names of attribute columns when you write data to a data table.

  • tableName: the name of the data table. This parameter is required.

  • primaryKeySchema: the schema of the primary key for the data table. This parameter is required. For more information, see Primary keys and attributes.

    • The primary key of a data table consists of one to four primary key columns. Primary key columns are sorted in the order in which they are added. For example, primary key (A, B, C) and primary key (A, C, B) have different schemas. Tablestore sorts rows based on the values of the primary key.

    • The first primary key column is the partition key. Data that has the same partition key value is stored in the same partition. We recommend that you set the size of data that has the same partition key to a value that is less than or equal to 10 GB. Otherwise, a single partition may be excessively large to split. We recommend that you evenly distribute requests to read and write data to different partition keys for load balancing.

  • definedColumnSchema: the names and types of the predefined columns. This parameter is optional. You cannot specify primary key columns as predefined columns. You can use the predefined columns of a data table as the attribute columns or index columns of an index table that is created for the data table.

    Important

    If you want to create a data table and a secondary index at the same time, you must configure this parameter.

tableOptions

No

The configuration information about the data table. For more information, see Data versions and TTL.

The configuration information includes the following parameters:

  • timeToLive: the retention period of data in the data table. Unit: seconds. If the retention period of data exceeds the time to live (TTL), the data expires. Tablestore automatically deletes expired data.

    The TTL must be at least 86,400 seconds (one day) or -1. A value of -1 specifies that the data never expires.

    When you create a data table, you can set the timeToLive parameter to -1. This way, the data in the data table never expires. After you create a data table, you can call the UpdateTable operation to modify the value of the timeToLive parameter.

    Note

    If you want to create an index table for the data table, the timeToLive parameter must meet one of the following conditions:

    • The timeToLive parameter of the data table is set to -1, which specifies that the data in the data table never expires.

    • The timeToLive parameter of the data table is set to a value other than -1 and the allowUpdate parameter is set to false.

  • maxVersions: the maximum number of versions of data that can be retained for a single attribute column. If the number of versions of data in an attribute column exceeds the value of the maxVersions parameter, the system automatically deletes data of earlier versions.

    When you create a data table, you can configure this parameter based on your business requirements. After you create a data table, you can call the UpdateTable operation to modify the value of the maxVersions parameter.

    Note

    If you want to create an index table for a data table, you must set the maxVersions parameter to 1 for the data table.

  • deviationCellVersionInSec: the max version offset, which is the maximum difference between the current system time and the timestamp of the written data. The difference between the version number and the time at which the data is written must be less than or equal to the value of the deviationCellVersionInSec parameter. Otherwise, an error occurs when the data is written.

    When you create a data table and do not configure the deviationCellVersionInSec parameter, Tablestore uses the default value of 86400 for the deviationCellVersionInSec parameter. After the data table is created, you can call the UpdateTable operation to modify the value of the deviationCellVersionInSec parameter.

    Unit: seconds.

  • allowUpdate: specifies whether to allow the UpdateRow operation. The default value is true, which specifies that the UpdateRow operation is allowed. If you set the allowUpdate parameter to false, the UpdateRow operation is prohibited.

reservedThroughput

No

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: CU.

  • If you set the reserved read throughput and reserved write throughout to a value that is greater than 0 for a data table, Tablestore reserves related resources for the data table. After you create the data table, you are charged for the reserved throughput resources. You are charged for additional throughput on a pay-as-you-go basis. For more information, see Billing overview.

  • If you set the reserved read throughput and reserved write throughout to 0, Tablestore does not reserve related resources for the data table.

indexMetas

No

The schema information about the index table. The schema information includes the following parameters:

  • indexName: the name of the index table.

  • primaryKey: the index columns of the index table, which are a combination of primary key columns and predefined columns of the data table.

  • definedColumns: the attribute columns of the index table. The attribute columns are a combination of predefined columns of the data table.

  • indexUpdateMode: the update mode of the index table. Only IUM_ASYNC_INDEX is supported.

  • indexType: the type of the index table. Only IT_GLOBAL_INDEX is supported.

Examples

Create a data table without creating an index table

The following sample code provides an example on how to create a data table that has two primary key columns and for which the reserved read throughput and reserved write throughput is set to 0.

// 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 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 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 need to 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 provides an example on how to create a data table and a global secondary index for the data table at the same time. In this example, the data table contains the Pk1 primary key column of the String type and the Pk2 primary key column of the String type. The Col1 column of the String type and the Col2 column of the String type are specified as predefined columns of the data table. The primary key columns of the global secondary index are Col1, Pk1, and Pk2. The attribute column of the global secondary index is Col2.

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 index column to Col1 and the attribute column to Col2 for 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);
}

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 Update a table.

  • 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.