All Products
Search
Document Center

Tablestore:Write time series data

Last Updated:Oct 08, 2024

You can call the PutTimeseriesData operation provided by Tablestore to write multiple rows of time series data at the same time. Time series data consists of metadata and data. If you do not define the time series metadata before you write time series data, the system automatically extracts the metadata of the time series and creates an index for the metadata.

Note

In the TimeSeries model of Tablestore, a two-dimensional time series table is used to store time series data. Each row represents the data at a point in time in a time series. The time series identifiers and timestamp are the primary key columns of the row, and the data points of the time series under the timestamp are the data columns of the row. A row can contain multiple data columns. For more information, see Overview.

Usage notes

We recommend that you evaluate the design of time series tables and check the total number of time series when you write time series data. In a short time series, only one row or a few rows of time series data exist. If a time series table contains a large number of short time series, you may find that the volume of time series metadata is large. Tablestore creates an index for the metadata of each time series. The total volume of metadata is calculated by assuming that the metadata size of each time series is 4 KB.

To delete time series metadata, you can configure time to live (TTL) to automatically delete the metadata. You can also call the DeleteTimeseriesMeta operation to manually delete the time series metadata. If you use Tablestore SDKs, TimeseriesKey specifies the time series identifiers.

Note

For more information about the billing of time series data, see Billable items of the TimeSeries model.

Features

You can call the PutTimeseriesData operation to write multiple rows of time series data at the same time. If a row already exists, data in the existing row is deleted and new data is written.

When you write time series data, you must specify time series identifiers and data in the time series. If you do not define metadata for a time series, the system automatically extracts the metadata of the time series based on the data that is written.

Methods

You can write time series data by using the Tablestore console, CLI, or SDKs.

Note

The features that are supported vary based on the method that you use to write time series data.

Before you write time series data, make sure that the following preparations are made:

Use the Tablestore console

You can define time series and write time series data to time series tables in the Tablestore console.

  1. Go to the Instance Management page.

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance.

  2. In the lower part of the Instance Details tab, click the Time Series Tables tab.

  3. On the Time Series Tables tab, click the name of the time series table that you want to manage, and then click the Query Data tab. You can also click Manage Data in the Actions column of the time series table.

  4. (Optional) Create a time series.

    1. On the Query Data tab, click Add Time Series.

    2. In the Add Time Series dialog box, configure the metadata of the time series.

      fig_datatimeseries

      The following table describes the parameters.

      Parameter

      Description

      Metric Name

      The name of a physical quantity or metric for the data in the time series, such as cpu or net, which specifies that the CPU usage or network usage is recorded in the time series.

      Data Source

      The identifier of the data source for the time series. This parameter can be left empty.

      Tag

      The tag of the time series. You can specify multiple key-value pairs of the string type.

      Property

      The property column of the time series, which is used to record some property information about the time series.

    3. Click Add.

  5. Insert data.

    1. Click Insert Data.

    2. In the Insert Data dialog box, configure the Time and Attribute Column parameters.fig_datatimeseries

    3. Click OK.

Use the Tablestore CLI

After you run the use --ts -t <TIMESERIES_TABLE> command to specify a time series table, you can use the Tablestore CLI to run the following commands to write time series data to the time series table.

  • Run the putts command to insert a row of data. For more information, see Write time series data.

    The following sample code provides an example on how to insert a row of time series data into a time series table:

    putts --k '["cpu","localhost",["region=hangzhou","os=ubuntu"]]' --field '[{"c":"fieldname","v":"fieldvalue"},{"c":"bool_field","v":true},{"c":"double_field","v":1.1},{"c":"int_value","v":10,"isint":true}]' --time 1635162859000000
  • Run the import_timeseries command to import time series data from a local file to the time series table. For more information, see Import time series data.

    Assume that the file is named import_timeseries.txt and stored in the /temp/ directory. Each row in the following example represents a row of time series data in the measurement_name,tags fields timestamp format.

    The following sample code shows the content of a configuration file:

    cpu,hostname=host_0,region=cn-hangzhou usage_user=58i,usage_system=2i,usage_idle=24i 1609459200000000000
    cpu,hostname=host_1,region=cn-hangzhou usage_user=58i,usage_system=2i,usage_idle=24i 1609459200000000000

    The following sample code provides an example on how to import time series data from the import_timeseries.txt file to a time series table:

    import_timeseries --input /temp/import_timeseries.txt

Use Tablestore SDKs

You can use Tablestore SDK for Java and Tablestore SDK for Go to write time series data. In this example, Tablestore SDK for Java is used.

The following sample code provides an example on how to write multiple rows of time series data to a time series table named test_timeseries_table:

private static void putTimeseriesData(TimeseriesClient client) {
    List<TimeseriesRow> rows = new ArrayList<TimeseriesRow>();
    for (int i = 0; i < 10; i++) {
        Map<String, String> tags = new HashMap<String, String>();
        tags.put("region", "hangzhou");
        tags.put("os", "Ubuntu16.04");
        // Specify the measurement name, data source, and tags of a time series to construct the identifiers of the time series. 
        TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "host_" + i, tags);
        // Specify the timeseriesKey and timeInUs parameters to create a row of time series data. 
        TimeseriesRow row = new TimeseriesRow(timeseriesKey, System.currentTimeMillis() * 1000 + i);
        // Add data values. 
        row.addField("cpu_usage", ColumnValue.fromDouble(10.0));
        row.addField("cpu_sys", ColumnValue.fromDouble(5.0));
        rows.add(row);
    }
    String tableName = "test_timeseries_table";
    PutTimeseriesDataRequest putTimeseriesDataRequest = new PutTimeseriesDataRequest(tableName);
    putTimeseriesDataRequest.setRows(rows);
    // Write multiple rows of time series data at the same time. 
    PutTimeseriesDataResponse putTimeseriesDataResponse = client.putTimeseriesData(putTimeseriesDataRequest);
    // Check whether all data is written to the time series table. 
    if (!putTimeseriesDataResponse.isAllSuccess()) {
        for (PutTimeseriesDataResponse.FailedRowResult failedRowResult : putTimeseriesDataResponse.getFailedRows()) {
            System.out.println(failedRowResult.getIndex());
            System.out.println(failedRowResult.getError());
        }
    }
}

FAQ

References

  • After you write time series data, you can perform the following operations:

  • Use Tablestore Sink Connector to migrate data from a Kafka data source to a time series table of Tablestore. For more information, see Data synchronization to time series tables.

  • You can manage time series metadata, such as retrieving time series, updating the properties in time series metadata, and deleting time series metadata. For more information, see Manage time series metadata.

  • To visualize time series data, you can connect Tablestore to Grafana. For more information, see Connect Tablestore to Grafana.