All Products
Search
Document Center

Tablestore:Write time series data

Last Updated:Dec 15, 2023

After you create a time series table, you can call the PutTimeseriesData operation to write multiple rows of time series data to the time series table at a time.

Prerequisites

Parameters

A row of time series data (timeseriesRow) includes a time series identifier (timeseriesKey) and time series data. The time series data includes data points (fields) and the time (timeInUs) of the data points. The following table describes the parameters.

Parameter

Description

timeseriesKey

The identifier of the time series. The identifier includes the following parameters:

  • measurementName: the measurement name of the time series.

  • dataSource: the data source of the time series. You can leave this parameter empty.

  • tags: the tags of the time series. A tags is a key-value pair of the STRING type.

timeInUs

The time of the data point. Unit: microseconds.

fields

The data points that consist of multiple pairs of names (FieldKey) and data values (FieldValue).

Sample code

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 identifier 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 a 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());
        }
    }
}