All Products
Search
Document Center

Tablestore:Create a time series table

Last Updated:Apr 21, 2025

You can call the CreateTimeseriesTable operation to create a time series table. When you create a time series table, you must specify the configuration information related to the time series table and time series metadata. Additionally, you can specify custom time series identifiers and custom data fields as the primary key columns of the time series table, create an analytical store, and create a Lastpoint index based on your business requirements.

Usage notes

  • Tablestore SDK for Python V6.1.0 and later support the TimeSeries model. Make sure that you use the correct version of Tablestore SDK for Python.

    Note

    For more information, see Version history of Tablestore SDK for Python.

  • The name of a time series table must be unique within an instance and cannot be the same as an existing table name.

  • You can create only one analytical store for a time series table. The total number of analytical stores and Lastpoint indexes cannot exceed 10.

    Note

    The analytical store and Lastpoint index features are supported in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), and China (Zhangjiakou).

  • You can specify up to four data fields as the primary key columns of a time series table.

  • When you specify custom time series identifiers for a time series table, you can add a maximum of six fields

Prerequisites

A Tablestore client is initialized. For more information, see Initialize a Tablestore client.

Parameters

The following table describes the parameters included in request.

Parameter

Description

table_meta (required)

The schema information of the time series table, which consists of the following items:

  • timeseries_table_name (required): the name of the time series table.

  • timeseries_table_options (optional): the configuration information of the time series table, which includes the following item:

    • time_to_live (required): the retention period of data in the time series table. Unit: seconds.

      The minimum retention period of data in a time series table is 86,400 seconds (one day). You can also set time_to_live to -1, which specifies that the data never expires.

  • timeseries_meta_options (optional): the configuration information of the time series metadata, which consists of the following items:

    • meta_time_to_live (optional): the retention period of the time series metadata. Unit: seconds.

      Default value: -1, which specifies that the time series metadata never expires. The minimum retention period of time series metadata is 604,800 seconds (seven days).

      Important

      The value of meta_time_to_live must be greater than or equal to the value of time_to_live.

    • allow_update_attributes (optional): specifies whether to allow updates to attributes of time series metadata. The default value is True, which specifies that updates to attributes of time series metadata are allowed.

      Important

      If meta_time_to_live is set to a value other than -1, you must set allow_update_attributes to False, which specifies that updates to attributes of time series metadata are not allowed.

  • timeseries_keys (optional): the custom time series identifiers. By default, the time series identifiers consist of the metric name, data source, and tags.

    Note

    You must specify this item only when you want to specify custom time series identifiers.

  • field_primary_keys (optional): the data fields specified as the primary key columns, which allow you to store multiple rows of data with the same time series identifiers and timestamp in a time series table.

    Note

    You must specify this item only when you want to store multiple rows of data with the same time series identifiers and timestamp at the same time.

analytical_stores (optional)

The configuration information of the analytical store for time series, which consists of the following items:

  • analytical_store_name (required): the name of the analytical store.

  • time_to_live (optional): the retention period of data in the analytical store. Unit: seconds.

    Default value: -1, which specifies that data in the analytical store never expires. The minimum retention period of data in an analytical store is 2,592,000 seconds (30 days).

  • sync_option (optional): the synchronization mode of the analytical store. Valid values:

    • SYNC_TYPE_FULL (default): full synchronization.

    • SYNC_TYPE_INCR: incremental synchronization.

lastpoint_index_metas (optional)

The configuration information of the Lastpoint index, which consists of the following item:

  • index_table_name (required): the name of the Lastpoint index.

Examples

Create a time series table

The following sample code provides an example on how to create a time series table:

try:
    # Set the retention period of data in the time series table to 172,800 seconds (two days).
    tableOption = TimeseriesTableOptions(172800)
    # Set the retention period of the time series metadata to -1 and specify that updates to attributes of time series metadata are allowed.
    metaOption = TimeseriesMetaOptions(-1, True)
    tableMeta = TimeseriesTableMeta("", tableOption, metaOption)

    # Call the operation to create the time series table.
    request = CreateTimeseriesTableRequest(tableMeta)
    otsClient.create_timeseries_table(request)
    print("create timeseries table success.")
except Exception as e:
    # If an exception is thrown, the time series table fails to be created. Handle the exception.
    print("create timeseries table failed. %s" % e)

Create a time series table with an analytical store

The following sample code provides an example on how to create a time series table with an analytical store:

try:
    # Set the retention period of data in the time series table to 172,800 seconds (two days).
    tableOption = TimeseriesTableOptions(172800)
    # Set the retention period of the time series metadata to -1 and specify that updates to attributes of time series metadata are allowed.
    metaOption = TimeseriesMetaOptions(-1, True)
    tableMeta = TimeseriesTableMeta("", tableOption, metaOption)

    # Configure an analytical store.
    analyticalStore = TimeseriesAnalyticalStore("default_analytical_store", -1, SyncType.SYNC_TYPE_FULL)

    # Call the operation to create the time series table.
    request = CreateTimeseriesTableRequest(tableMeta, [analyticalStore])
    otsClient.create_timeseries_table(request)
    print("create timeseries table success.")
except Exception as e:
    # If an exception is thrown, the time series table fails to be created. Handle the exception.
    print("create timeseries table failed. %s" % e)

Create a time series table with custom time series identifiers and custom data fields specified as the primary key columns

The following sample code provides an example on how to create a time series table with custom time series identifiers keyA, keyB, and keyC, and two data fields gid (String type) and uid (Integer type) specified as the primary key columns of the time series table:

try:
    # Set the retention period of data in the time series table to 86,400 seconds (one day).
    tableOption = TimeseriesTableOptions(86400)
    # Set the retention period of the time series metadata to -1 and specify that updates to attributes of time series metadata are allowed.
    metaOption = TimeseriesMetaOptions(-1, True)
    # Specify custom time series identifiers
    timeseriesKeys = ["keyA", "keyB", "keyC"]
    # Specify custom data fields as the primary key columns of the time series table.
    fieldPrimaryKeys = [('gid', 'STRING'), ('uid', 'INTEGER')]
    tableMeta = TimeseriesTableMeta("", tableOption, metaOption, timeseriesKeys,
                                    fieldPrimaryKeys)

    # Call the operation to create the time series table.
    request = CreateTimeseriesTableRequest(tableMeta)
    otsClient.create_timeseries_table(request)
    print("create timeseries table success.")
except Exception as e:
    # If an exception is thrown, the time series table fails to be created. Handle the exception.
    print("create timeseries table failed. %s" % e)

Create a time series table with a Lastpoint index

The following sample code provides an example on how to create a time series table with a Lastpoint index:

try:
    # Set the retention period of data in the time series table to 86,400 seconds (one day).
    tableOption = TimeseriesTableOptions(86400)
    # Set the retention period of the time series metadata to 604,800 seconds (seven days) and specify that updates to attributes of time series metadata are not allowed.
    metaOption = TimeseriesMetaOptions(604800, False)
    tableMeta = TimeseriesTableMeta("", tableOption, metaOption)

    # Configure a Lastpoint index.
    lastPointIndex = LastpointIndexMeta("")

    # Call the operation to create the time series table.
    request = CreateTimeseriesTableRequest(tableMeta, None, [lastPointIndex])
    otsClient.create_timeseries_table(request)
    print("create timeseries table success.")
except Exception as e:
    # If an exception is thrown, the time series table fails to be created. Handle the exception.
    print("create timeseries table failed. %s" % e)