All Products
Search
Document Center

Tablestore:Create a data table

Last Updated:Aug 29, 2024

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 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 operation

/**
 * Create a data table based on the specified table schema. 
 */
createTable(params, callback)

Parameters

The following table describes the parameters that you can configure when you call the CreateTable operation to create a data table.

Parameter

description

tableMeta

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

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

  • primaryKey: This parameter is optional and specifies the primary key of the data table. For more information, see Primary keys and attributes.

    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.

    • 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 all primary key columns.

    • The first primary key column is the partition key. Data that has the same partition key is stored in the same partition. We recommend that you keep the size of data with the same partition key less than or equal to 10 GB. Otherwise, a single partition may be too large to split. We also recommend that you evenly distribute read/write operations among different partition keys to facilitate load balancing.

  • definedColumn: the predefined columns of the data table and the data types of the predefined columns. Primary key columns cannot be specified as predefined columns. You can use predefined columns as the index columns or attribute columns for index tables.

tableOptions

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

The configuration information includes the following parameters:

  • timeToLive: the period during which data in the table can be retained. This period is the validity period of data. If the retention period exceeds the value of the timeToLive parameter, Tablestore automatically deletes expired data.

    The minimum time_to_live value is 86400, which is equal to one day. A value of -1 indicates that the data never expires.

    If you set the timeToLive parameter to -1 for the data table, the data in the data table never expires. After the data table is created, you can call the UpdateTable operation to modify the value of the timeToLive parameter.

    Unit: seconds.

    Important

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

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

    • The timeToLive parameter of the data table is set to a value other than -1 and update operations on the data table are prohibited.

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

    Important

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

  • 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

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

  • If you set the reserved read throughput or reserved write throughput to a value that is greater than 0 for a data table, Tablestore reserves the 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 or reserved write throughput to 0, Tablestore does not reserve resources for the data table.

indexMetas

The schema information about the index tables. Each indexMeta includes the following parameters:

  • name: the name of the index table.

  • primaryKey: the primary key of the index table. The primary key is a combination of all primary key columns and a random number of predefined columns of the data table.

    If you want to create a local secondary index, the first primary key column of the index table must be the same as the first primary key column of the data table.

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

  • includeBaseData: specifies whether to include the existing data of the data table in the index table.

    If you set the includeBaseData parameter to true, the index table includes the existing data of the data table. If you set the includeBaseData parameter to false, the index table does not include the existing data of the data table.

  • indexType: the type of the index table. Valid values: IT_GLOBAL_INDEX and IT_LOCAL_INDEX.

    • If you do not configure the indexType parameter or set the indexType parameter to IT_GLOBAL_INDEX, a global secondary index is created. For more information, see Create a secondary index.

      Tablestore automatically synchronizes data from the indexed columns and primary key columns of the data table to the columns of the index table that you want to create in asynchronous mode. The synchronization latency is within a few milliseconds.

    • If you set the indexType parameter to IT_LOCAL_INDEX, a local secondary index is created. For more information, see Create a secondary index.

      Tablestore automatically synchronizes data from the indexed columns and primary key columns of the data table to the columns of the index table that you want to create in synchronous mode. After data is written to the data table, you can immediately query the data in the index table.

  • indexUpdateMode: the update mode of the index table. Valid values: IUM_ASYNC_INDEX and IUM_SYNC_INDEX.

    • If you do not configure the indexUpdateMode parameter or set the indexUpdateMode parameter to IUM_ASYNC_INDEX, the asynchronous update mode is used to update the index.

      If you want to use the global secondary index feature, you must set the indexUpdateMode parameter to IUM_ASYNC_INDEX.

    • If you set the indexUpdateMode parameter to IUM_SYNC_INDEX, the synchronous update mode is used to update the index.

      If you want to use the local secondary index feature, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX.

Examples

Create a data table without creating an index table

The following sample code provides an example on how to create a data table without creating an index table for the data table. In this example, the data table consists of two primary key columns and 0 is specified as the reserved read throughput and reserved write throughput for the data table.

var client = require('./client');

var params = {
  tableMeta: {
    tableName: 'sampleTable',
    primaryKey: [
      {
        name: 'gid',
        type: 'INTEGER'
      },
      {
        name: 'uid',
        type: 'INTEGER'
      }
    ]
  },
  reservedThroughput: {
    capacityUnit: {
      read: 0,
      write: 0
    }
  },
  tableOptions: {
    timeToLive: -1, // Specify the validity period of data in seconds. A value of -1 specifies that the data never expires. If you want to set the validity period to one year, set the timeToLive parameter to 31536000. The value is calculated by using the following formula: 365 × 24 × 3,600. 
    maxVersions: 1 // Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. 
  }
};

client.createTable(params, function (err, data) {
  if (err) {
    console.log('error:', err);
    return;
  }
  console.log('success:', data);
});

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. In this example, the data table consists of the pk1 primary key column of the Integer type and the pk2 primary key column of the Integer type. The col1 column of the Integer type and the col2 column of the Integer type are specified as predefined columns of the data table. Only the latest version of data is retained for each attribute column in the data table and data in the data table never expires. The global secondary index named sdkGlobalIndex2 consists of the col1, pk1, and pk2 primary key columns and the col2 attribute column.

var client = require('./client');
var TableStore = require('../index.js');

var params = {
  tableMeta: {
    tableName: 'sdkGlobalTest',
    primaryKey: [
      {
        name: 'pk1',
        type: TableStore.PrimaryKeyType.INTEGER
      },
      {
        name: 'pk2',
        type: TableStore.PrimaryKeyType.INTEGER
      }
    ],
    definedColumn: [
      {
        "name": "col1",
        "type": TableStore.DefinedColumnType.DCT_INTEGER
      },
      {
        "name": "col2",
        "type": TableStore.DefinedColumnType.DCT_INTEGER
      }
    ],
  },
  reservedThroughput: {
    capacityUnit: {
      read: 0,
      write: 0
    }
  },
  tableOptions: {
    timeToLive: -1, // Specify the validity period of data in seconds. A value of -1 specifies that the data never expires. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    maxVersions: 1 // Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
  },
  streamSpecification: {
    enableStream: false, // The Stream feature cannot be enabled for secondary indexes. 
  },
  indexMetas: [
    {
      name: "sdkGlobalIndex1",
      primaryKey: ["pk2"],
      definedColumn: ["col1", "col2"]
    },
    {
      name: "sdkGlobalIndex2",
      primaryKey: ["col1"],
      definedColumn: ["col2"]
    }
  ]
};

client.createTable(params, function (err, data) {
  if (err) {
    console.log('error:', err);
    return;
  }
  console.log('success:', data);
});

Create a data table and a local secondary index

The following sample code provides an example on how to create a data table and a local secondary index for the data table at the same time. In this example, the data table consists of the pk1 primary key column of the Integer type and the pk2 primary key column of the Integer type. The col1 column of the Integer type and the col2 column of the Integer type are specified as predefined columns of the data table. Only the latest version of data is retained for each attribute column in the data table and data in the data table never expires. The local secondary index named sdklocalIndex1 consists of the pk1, col1, and pk2 primary key columns and the col2 attribute column.

var client = require('./client');
var TableStore = require('../index.js');

var params = {
  tableMeta: {
    tableName: 'sdkLocalTest',
    primaryKey: [
      {
        name: 'pk1',
        type: TableStore.PrimaryKeyType.INTEGER
      },
      {
        name: 'pk2',
        type: TableStore.PrimaryKeyType.INTEGER
      }
    ],
    definedColumn: [
      {
        "name": "col1",
        "type": TableStore.DefinedColumnType.DCT_INTEGER
      },
      {
        "name": "col2",
        "type": TableStore.DefinedColumnType.DCT_INTEGER
      }
    ],
  },
  reservedThroughput: {
    capacityUnit: {
      read: 0,
      write: 0
    }
  },
  tableOptions: {
    timeToLive: -1, // Specify the validity period of data in seconds. A value of -1 specifies that the data never expires. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    maxVersions: 1 // Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
  },
  streamSpecification: {
    enableStream: false, // The Stream feature cannot be enabled for secondary indexes. 
  },
  indexMetas: [
    {
      name: "sdklocalIndex1",
      primaryKey: ["pk1","col1"], // Add primary key columns to the index table. The first primary key column of the index table must be the same as the first primary key column of the data table. 
      definedColumn: ["col2"],
      indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX, // Set the indexUpdateMode parameter to IUM_SYNC_INDEX, which specifies the synchronous update mode. If you set the indexType parameter to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX. 
      indexType: TableStore.IndexType.IT_LOCAL_INDEX, // Set the indexType parameter to IT_LOCAL_INDEX, which specifies local secondary index. 
    },

    {
      name: "sdklocalIndex2",
      primaryKey: ["pk1","col2"],
      definedColumn: ["col1"],
      indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX, // Set the indexUpdateMode parameter to IUM_SYNC_INDEX, which specifies the synchronous update mode. If you set the indexType parameter to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX. 
      indexType: TableStore.IndexType.IT_LOCAL_INDEX, // Set the indexType parameter to IT_LOCAL_INDEX, which specifies local secondary index. 
    }
  ]
};

client.createTable(params, function (err, data) {
  if (err) {
    console.log('error:', err);
    return;
  }
  console.log('success:', data);
});

References

  • For information about the API operation that you can call to create a data table, see CreateTable. For the detailed sample code, visit CreateTable@GitHub.

  • You can call API operations to read and write data in a table. For more information, see Basic operations on data.

  • You can update a table to modify the information about the table, such as the time to live (TTL) and max versions. For more information, see Update a table.

  • After you enable the local transaction feature for a data table, you can create a local transaction and perform read and write operations on the data in the local transaction. For more information, see Local transaction.

  • You can query the names of tables to view all existing tables in an instance. For more information, see Query 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 tables.