Specify custom time series identifiers and custom data fields as the primary key columns

Updated at: 2025-04-24 09:00
important

This topic contains important information on necessary precautions. We recommend that you read this topic carefully before proceeding.

By default, the time series identifiers of time series in the TimeSeries model consist of the metric name, data source, and tags. You can specify custom time series identifiers based on your business requirements. In a time series table, a row of time series data is uniquely identified by the time series identifiers and timestamp. You can specify custom data fields as the primary key columns to store multiple rows with the same time series identifiers and timestamp but different time series data. This topic describes how to specify custom time series identifiers and custom data fields as the primary key columns of a time series table.

Usage notes

You can specify custom time series identifiers and custom data fields as the primary key columns of a time series table in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), and China (Zhangjiakou). To specify custom time series identifiers and custom data fields as the primary key columns of a time series table, create an instance for the TimeSeries model in one of the preceding regions.

Scenarios

Specify custom time series identifiers
Specify custom data fields as the primary key columns

You may want to specify custom time series identifiers in scenarios that require precise time positioning, range queries, trend analysis, and event association. The following items describe the scenarios:

  • Troubleshooting and root cause analysis

    In IT O&M and industrial equipment monitoring, custom time series identifiers help precisely mark when failures occur. Combined with other time series data (such as device status and log information), custom time series identifiers help quickly locate problem causes for root cause analysis.

  • User behavior analysis

    In Internet applications, custom time series identifiers enable the analysis of user active periods, session durations, and behavior paths by recording user login time, page view durations, and operation sequences. This data-driven approach facilitates user experience optimization and intelligent content recommendations.

  • Financial transaction auditing

    In the financial sector, each transaction requires a precise timestamp. Custom time series identifiers are used not only to ensure the sequence and integrity of transactions but also for anti-fraud analysis, such as identifying abnormal transaction patterns and tracking fund flows.

By specifying custom time series identifiers, you can group, filter, and analyze time series data in a flexible manner, enabling fine-grained management and deeper insights across diverse business scenarios.

Sample time series data

The following table shows the sample time series data for monitoring the temperature and humidity of industrial devices.

  • The measurement, data_source, area, and _tags columns are fields of custom time series identifiers, which store the metric name, data source, region, and tags, respectively.

  • The _time column stores the timestamp.

  • The temperature and humidity columns store the temperature and humidity, respectively.

measurement

data_source

area

_tags

_time

temperature

humidity

temperature

sensorA001

production

["sensor_type=typeA",

"sensor_version=1.0"]

1712476514000000

24.5

temperature

sensorA002

production

["sensor_type=typeA",

"sensor_version=1.0"]

1712476524000000

24.5

temperature

sensorA002

maintenance

["sensor_type=typeA",

"sensor_version=1.1"]

1712476534000000

23.0

humidity

sensorB001

production

["sensor_type=typeB",

"sensor_version=1.0"]

1712476514000000

0.55

humidity

sensorB001

maintenance

["sensor_type=typeB",

"sensor_version=1.0"]

1712476524000000

0.50

If you want to store multiple rows of time series data with the same time series identifiers and timestamp, you can specify custom data fields as the primary key columns of your time series table. The following items describe the scenarios:

  • Event association analysis

    In some complex analysis scenarios, you may want to associate time series data based on data fields other than time series identifiers. You can specify custom data fields as the primary key columns to quickly locate data of a specific device at specific timestamps. This approach is suitable for troubleshooting and behavior analysis.

  • Multi-dimensional indexing

    For advanced applications of time series databases, if you want to perform multi-dimensional queries and filtering on data, you can specify custom data fields as the primary key columns to improve query efficiency. This approach enables more precise location of relevant time series data especially when you process large amounts of data.

  • Multi-tenant time series data management

    When you provide time series data storage services for multiple customers, you may want to specify the tenant ID as one of the primary key columns in the time series table to form a composite primary key with the timestamp. This approach ensures data isolation and efficient access for different tenants.

Sample time series data

When you monitor server CPU utilization, you need to use time series identifiers to locate each server, but each server has multiple cores. In this case, you can specify the core ID as a primary key column. The following table shows the sample time series data.

  • The _m_name, _data_source, and _tags columns are fields of custom time series identifiers, which store the metric name, data source, and tags, respectively.

  • The _time column stores the timestamp.

  • The processor column stores the server core ID. This data field is specified as a primary key column.

  • The cpu_usage column stores the CPU utilization.

_m_name

_data_source

_tags

_time

processor

cpu_usage

cpu

host_1

["region=hangzhou"]

1712476524000000

0

10.0

cpu

host_1

["region=hangzhou"]

1712476524000000

1

10.0

cpu

host_1

["region=hangzhou"]

1712476524000000

2

10.0

Methods

You can specify custom time series identifiers and custom data fields as the primary key columns of a time series table in the Tablestore console or by using Tablestore SDKs.

Before you specify custom time series identifiers and custom data fields as the primary key columns of a time series table, make sure that the following preparations are made:

  • An Alibaba Cloud account or a Resource Access Management (RAM) user that has Tablestore operation permissions is created. For information about how to grant Tablestore operation permissions to a RAM user, see Use a RAM policy to grant permissions to a RAM user.

    When you use Tablestore SDKs, if no AccessKey pair is available, you must create an AccessKey pair for the Alibaba Cloud account or RAM user. For more information, see Create an AccessKey pair.

    Warning

    If the AccessKey pair of your Alibaba Cloud account is leaked, the security of all your resources will be compromised. To ensure account security, we recommend that you use the AccessKey pair of a RAM user and do not create and use the AccessKey pair of an Alibaba Cloud account.

  • An instance for the TimeSeries model is created. For more information, see Create an instance for the TimeSeries model.

    Important

    To specify custom time series identifiers and custom data fields as the primary key columns of a time series table, you must create an instance for the TimeSeries model in the China (Hangzhou), China (Shanghai), China (Beijing), or China (Zhangjiakou) region.

  • When you use Tablestore SDKs, a TimeseriesClient is initialized. For more information, see Initialize a Tablestore client.

Specify custom time series identifiers

By default, the time series identifiers of time series in the TimeSeries model consist of the metric name, data source, and tags. In a time series table, these time series identifiers correspond to the _m_name, _data_source, and _tags fields. Tags consist of multiple key-value pairs of the String type.

When the default field names of time series identifiers do not meet your business requirements, you can specify custom time series identifiers to better fit actual application scenarios, improving the intuitiveness and efficiency of data processing and analysis. For example, when you monitor server CPU utilization, you can use time series identifiers to locate each CPU core. After you specify custom time series identifiers, you can still use the default names of time series identifiers for data operations.

You must specify custom time series identifiers when you create a time series table. Then, you can write data to the time series table based on the custom time series identifiers. The specific process is as follows:

Use the Tablestore console
Use Tablestore SDKs
  1. When you create a time series table, specify custom time series identifiers. For more information, see Operations on time series tables.

  2. Write data to the time series table based on the custom time series identifiers.

    When you write data to the time series table, the fields of the custom time series identifiers on the client are passed to the Tablestore server as key-value pairs. The Tablestore server first extracts the metric name and data source from the key-value pairs, and then uses the remaining key-value pairs as tags of the time series. For more information, see Write time series data.

    Important

    If the fields of the custom time series identifiers do not include the _tags field when you create the time series table but there are still key-value pairs remaining after excluding the metric name, data source, and custom fields from the key-value pairs when you write time series data, the data fails to be written.

  3. Read data from the time series table.

    1. Create an SQL mapping table for the time series table. For more information, see Create mapping tables in the multi-value model for time series tables.

      1. On the Manage Time Series Table page, click the Query by Executing SQL Statement tab.

      2. On the left side of the Query by Executing SQL Statement tab, click the + icon next to the name of the instance for the TimeSeries model.

      3. In the Create Mapping Table dialog box, configure the parameters. The following table describes the parameters.

        Parameter

        Description

        Table Name

        The name of the time series table.

        Mapping Table Name

        The name of the mapping table.

        Note

        When you create an SQL mapping table for a time series table, the system automatically adds the prefix time series table name:: to the mapping table name.

        Enable Analytical Store

        The analytical store feature is used in scenarios in which you want to store time series data for a long period of time and analyze time series data. You can use the analytical store feature to store time series data at lower costs and query and analyze time series data in an efficient manner. For more information, see Analytical store for time series.

        By default, Enable Analytical Store is turned off. Configure the parameter based on your business requirements.

        • If you do not want to use an analytical store to store and analyze time series data, retain the default setting for the Enable Analytical Store parameter.

        • If you want to store time series data at lower costs or query and analyze time series data in an efficient manner, make sure that an analytical store is created and Enable Analytical Store is turned on.

        Important
        • The analytical store feature is supported in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), and China (Zhangjiakou). To use the analytical store feature, create an instance for the TimeSeries model in one of the preceding regions. For more information, see Create an instance for the TimeSeries model.

        • If you create a mapping table for which Enable Analytical Store is turned on, when you delete the analytical store, you must delete the mapping table at the same time. Otherwise, you fail to delete the analytical store.

      4. Click Generate SQL Statement. The system automatically generates the SQL statement to create the mapping table.

        After you specify custom time series identifiers, when you create an SQL mapping table, specify the custom time series identifiers and custom data fields that are used as the primary key columns of the time series table as the primary key columns of the SQL mapping table.

      5. Click Execute SQL Statement(F8).

    2. Execute the SELECT statement to query data. For more information, see Query data.

      Enter the SQL statement in the SQL editor in the console and click Execute SQL Statement(F8).

      The following sample SQL statement provides an example on how to query information of the time series in which the cpu_usage value exceeds 90.0 in a time series table by using the mapping table named cpu::ts1:

      SELECT `host`, `processor`, `_tags` , `_time` FROM `cpu::ts1` WHERE cpu_usage > 90.0;
Important
  • You can specify custom time series identifiers by using Tablestore SDK for Java V5.17.1 and later, the latest version of Tablestore SDK for Go, or Tablestore SDK for Python V6.1.0 and later. When you specify custom time series identifiers, make sure you use a correct version of the Tablestore SDK.

  • When you write time series data based on custom time series identifiers, if you do not want to add a metric name or data source, leave the corresponding field empty.

In this example, Tablestore SDK for Java is used.

  1. When you create a time series table, specify custom time series identifiers.

    The following sample code provides an example on how to create a time series table without an analytical store and specify custom time series identifiers:

    private static void createTimeseriesTable(TimeseriesClient client) {
        TimeseriesTableMeta timeseriesTableMeta = new TimeseriesTableMeta("");
        timeseriesTableMeta.setTimeseriesTableOptions(new TimeseriesTableOptions(-1));
        // Specify custom time series identifiers. The host field stores the metric name, the processor field stores the data source, and the _tags field stores the tags.
        timeseriesTableMeta.addTimeseriesKey("host");
        timeseriesTableMeta.addTimeseriesKey("processor");
        timeseriesTableMeta.addTimeseriesKey("_tags");
        CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(timeseriesTableMeta);
        request.setEnableAnalyticalStore(false);
        client.createTimeseriesTable(request);
    }
  2. Write data to the time series table based on the custom time series identifiers.

    When you write data to the time series table, the fields of the custom time series identifiers on the client are passed to the Tablestore server as key-value pairs. The Tablestore server first extracts the metric name and data source from the key-value pairs, and then uses the remaining key-value pairs as tags of the time series.

    Important

    If the fields of the custom time series identifiers do not include the _tags field when you create the time series table but there are still key-value pairs remaining after excluding the metric name, data source, and custom fields from the key-value pairs when you write time series data, the data fails to be written.

    The following sample code provides an example on how to write three rows of time series data to a time series table based on the custom time series identifiers.

    private static void putTimeseriesData(TimeseriesClient client) {
        long timeInUs = System.currentTimeMillis() * 1000;
        List rows = new ArrayList();
        for (int i = 0; i < 3; i++) {
            Map tags = new HashMap<>();
            // Add fields of the custom time series identifiers.
            tags.put("host", "host_" + i);
            tags.put("processor", String.valueOf(i));
            // Key-value pairs that do not correspond to custom time series identifiers are used as tags of the time series.
            tags.put("region", "hangzhou");
             TimeseriesKey timeseriesKey = new TimeseriesKey("", "", tags);
            TimeseriesRow row = new TimeseriesRow(timeseriesKey, timeInUs);
            row.addField("cpu_usage", ColumnValue.fromDouble(10.0));
            row.addField("cpu_sys", ColumnValue.fromDouble(5.0));
            rows.add(row);
        }
        String tableName = "";
        PutTimeseriesDataRequest putTimeseriesDataRequest = new PutTimeseriesDataRequest(tableName);
        putTimeseriesDataRequest.setRows(rows);
        client.putTimeseriesData(putTimeseriesDataRequest);
    }

    The following table shows the time series data in the time series table after the data is successfully written.

    host

    processor

    _tags

    _time

    cpu_usage

    cpu_sys

    host_0

    0

    ["region=hangzhou"]

    1712476524000000

    10.0

    5.0

    host_1

    1

    ["region=hangzhou"]

    1712476524000000

    10.0

    5.0

    host_2

    2

    ["region=hangzhou"]

    1712476524000000

    10.0

    5.0

  3. Execute SQL statements to read data from the time series table.

    1. Create an SQL mapping table for the time series table. For more information, see Create mapping tables in the multi-value model for time series tables.

      After you specify custom time series identifiers, when you create an SQL mapping table, specify the fields of the custom time series identifiers and the timestamp field as the primary key columns of the SQL mapping table.

      Sample configurations:

      CREATE TABLE `cpu::ts1` (
          `host` VARCHAR(1024),
          `processor` VARCHAR(1024),
          `_tags` VARCHAR(1024),
          `_time` BIGINT(20),
          `cpu_usage` DOUBLE,
          `cpu_sys` DOUBLE,
          PRIMARY KEY(`host`,`processor`,`_tags`,`_time`)
      );
    2. Execute the SELECT statement to query data. For more information, see Query data.

      The following sample SQL statement provides an example on how to query information of the time series in which the cpu_usage value exceeds 90.0 in a time series table by using the mapping table named cpu::ts1:

      SELECT `host`, `processor`, `_tags` , `_time` FROM `cpu::ts1` WHERE cpu_usage > 90.0;

Specify custom data fields as the primary key columns

In a time series table, a row of time series data is uniquely identified by the time series identifiers and timestamp. This indicates that two rows of time series data cannot have the same time series identifiers and timestamp.

If you want to store multiple rows of time series data with the same time series identifiers and timestamp, you can specify custom data fields as the primary key columns of your time series table. For example, when you monitor server CPU utilization, you need to use time series identifiers to locate each server, but each server has multiple cores. In this case, you can specify the core ID as a primary key column.

You must first specify custom data fields as the primary key columns of a time series table when you create the time series table. Then, you must specify the values of the data fields that are specified as the primary key columns when you write data to the time series table. The specific process is as follows:

Use the Tablestore console
Use Tablestore SDKs
  1. When you create a time series table, specify custom data fields as the primary key columns of the time series table. For more information, see Operations on time series tables.

  2. Write time series data to the time series table.

    When you write data to a time series table, the data fields that are specified as the primary key columns cannot be left empty. You can store multiple rows of time series data with the same time series identifiers and timestamp by specifying custom data fields as the primary key columns. For more information, see Write time series data.

  3. Read data from the time series table.

    1. Create an SQL mapping table for the time series table. For more information, see Create mapping tables in the multi-value model for time series tables.

      1. On the Manage Time Series Table page, click the Query by Executing SQL Statement tab.

      2. On the left side of the Query by Executing SQL Statement tab, click the + icon next to the name of the instance for the TimeSeries model.

      3. In the Create Mapping Table dialog box, configure the parameters. The following table describes the parameters.

        Parameter

        Description

        Table Name

        The name of the time series table.

        Mapping Table Name

        The name of the mapping table.

        Note

        When you create an SQL mapping table for a time series table, the system automatically adds the prefix time series table name:: to the mapping table name.

        Enable Analytical Store

        The analytical store feature is used in scenarios in which you want to store time series data for a long period of time and analyze time series data. You can use the analytical store feature to store time series data at lower costs and query and analyze time series data in an efficient manner. For more information, see Analytical store for time series.

        By default, Enable Analytical Store is turned off. Configure the parameter based on your business requirements.

        • If you do not want to use an analytical store to store and analyze time series data, retain the default setting for the Enable Analytical Store parameter.

        • If you want to store time series data at lower costs or query and analyze time series data in an efficient manner, make sure that an analytical store is created and Enable Analytical Store is turned on.

        Important
        • The analytical store feature is supported in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), and China (Zhangjiakou). To use the analytical store feature, create an instance for the TimeSeries model in one of the preceding regions. For more information, see Create an instance for the TimeSeries model.

        • If you create a mapping table for which Enable Analytical Store is turned on, when you delete the analytical store, you must delete the mapping table at the same time. Otherwise, you fail to delete the analytical store.

      4. Click Generate SQL Statement. The system automatically generates the SQL statement to create the mapping table.

        After you specify custom data fields as the primary key columns of a time series table, when you create an SQL mapping table, specify the time series identifiers and custom data fields that are used as the primary key columns of the time series table as the primary key columns of the SQL mapping table.

      5. Click Execute SQL Statement(F8).

    2. Execute the SELECT statement to query data. For more information, see Query data.

      Enter the SQL statement in the SQL editor in the console and click Execute SQL Statement(F8).

      The following sample SQL statement provides an example on how to query information of the time series in which the cpu_usage value exceeds 90.0 in a time series table by using the mapping table named cpu::ts1:

      SELECT `host`, `processor`, `_tags` , `_time` FROM `cpu::ts1` WHERE cpu_usage > 90.0;
Important

You can specify custom data fields as the primary key columns by using Tablestore SDK for Java V5.17.1 and later, the latest version of Tablestore SDK for Go, or Tablestore SDK for Python V6.1.0 and later. When you specify custom data fields as the primary key columns, make sure you use a correct version of the Tablestore SDK.

In this example, Tablestore SDK for Java is used.

  1. When you create a time series table, specify custom data fields as the primary key columns of the time series table.

    The following sample code provides an example on how to create a time series table without an analytical store and specify custom data fields as the primary key columns of the time series table:

    private static void createTimeseriesTable(TimeseriesClient client) {
        TimeseriesTableMeta timeseriesTableMeta = new TimeseriesTableMeta("");
        timeseriesTableMeta.setTimeseriesTableOptions(new TimeseriesTableOptions(-1));
        // Specify custom data fields as the primary key columns.
        timeseriesTableMeta.addFieldPrimaryKey("processor", PrimaryKeyType.INTEGER);
        CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(timeseriesTableMeta);
        request.setEnableAnalyticalStore(false);
        client.createTimeseriesTable(request);
    }
  2. Write time series data to the time series table.

    When you write data to a time series table, the data fields that are specified as the primary key columns cannot be left empty. You can store multiple rows of time series data with the same time series identifiers and timestamp by specifying custom data fields as the primary key columns.

    The following sample code provides an example on how to write three rows of time series data with the same time series identifiers and timestamp to a time series table:

    private static void putTimeseriesData(TimeseriesClient client) {
        long timeInUs = System.currentTimeMillis() * 1000;
        List rows = new ArrayList();
        for (int i = 0; i < 3; i++) {
            Map tags = new HashMap<>();
            tags.put("region", "hangzhou");
             TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "host_" + i, tags);
            TimeseriesRow row = new TimeseriesRow(timeseriesKey, timeInUs);
            // The data fields that are specified as the primary key columns cannot be left empty.
            row.addField("processor", ColumnValue.fromLong(i));
            row.addField("cpu_usage", ColumnValue.fromDouble(10.0));
            row.addField("cpu_sys", ColumnValue.fromDouble(5.0));
            rows.add(row);
        }
        String tableName = "<TIMESERIES_TABLE_NAME>";
        PutTimeseriesDataRequest putTimeseriesDataRequest = new PutTimeseriesDataRequest(tableName);
        putTimeseriesDataRequest.setRows(rows);
        client.putTimeseriesData(putTimeseriesDataRequest);
    }

    The following table shows the time series data in the time series table after the data is successfully written.

    _m_name

    _data_source

    _tags

    _time

    processor

    cpu_usage

    cpu_sys

    cpu

    host_0

    ["region=hangzhou"]

    1712476524000000

    0

    10.0

    5.0

    cpu

    host_1

    ["region=hangzhou"]

    1712476524000000

    1

    10.0

    5.0

    cpu

    host_2

    ["region=hangzhou"]

    1712476524000000

    2

    10.0

    5.0

  3. Execute SQL statements to read data from the time series table.

    1. Create an SQL mapping table for the time series table. For more information, see Create mapping tables in the multi-value model for time series tables.

      After you specify custom data fields as the primary key columns of a time series table, when you create an SQL mapping table, specify the time series identifiers and custom data fields that are used as the primary key columns of the time series table as the primary key columns of the SQL mapping table.

      Sample configurations:

      CREATE TABLE `cpu::ts1` (
          `_m_name` VARCHAR(1024),
          `_data_source`  VARCHAR(1024),
          `_tags` VARCHAR(1024),
          `_time` BIGINT(20),
          `processor` BIGINT,
          `cpu_usage` DOUBLE,
          `cpu_sys` DOUBLE,
          PRIMARY KEY(`_m_name`,`_data_source`,`_tags`,`_time`,`processor`)
      );
    2. Execute the SELECT statement to query data. For more information, see Query data.

      The following sample SQL statement provides an example on how to query information of the time series in which the cpu_usage value exceeds 90.0 in a time series table by using the mapping table named cpu::ts1:

      SELECT `_m_name`, `_data_source`, `_tags` , `_time`, `processor` FROM `cpu::ts1` WHERE cpu_usage > 90.0;
  • On this page (1)
  • Usage notes
  • Scenarios
  • Methods
  • Specify custom time series identifiers
  • Specify custom data fields as the primary key columns
Feedback
phone Contact Us

Chat now with Alibaba Cloud Customer Service to assist you in finding the right products and services to meet your needs.

alicare alicarealicarealicare