SDK data ingestion

更新时间:
复制 MD 格式

After you create a time series table using a software development kit (SDK), you can write time series data to Tablestore. This topic describes how to use the Tablestore SDK to create a time series table and write time series data to it.

Background information

Time series models are designed for scenarios such as Internet of Things (IoT) device monitoring, device data collection, and machine monitoring.

Use an SDK

You can use the time series model with SDKs for the following languages. This topic uses the Java SDK as an example.

Step 1: Create a time series table

Call the CreateTimeseriesTable operation to create a time series table to store time series data.

Note

To store time series data at a low cost and quickly query and analyze the data, you can create an analytical store for the time series table. The analytical store feature is used for the long-term storage and analysis of time series data.

The following sample code shows how to create a time series table named test_timeseries_table whose data never expires. For more information about the structure of a time series table, see TimeseriesTableMeta.

private static void createTimeseriesTable(TimeseriesClient client) {
    String tableName = "test_timeseries_table";
    TimeseriesTableMeta timeseriesTableMeta = new TimeseriesTableMeta(tableName);
    int timeToLive = -1;
    timeseriesTableMeta.setTimeseriesTableOptions(new TimeseriesTableOptions(timeToLive));
    CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(timeseriesTableMeta);
    // Do not create a default analytical store.
    request.setEnableAnalyticalStore(false);    
    client.createTimeseriesTable(request);
}

Step 2: Write time series data

Call the PutTimeseriesData operation to write time series data to a time series table in batches. You can write data for multiple rows in a single PutTimeseriesData call.

The following example shows how to write multiple rows of time series data to a time series table named test_timeseries_table. For more information about the structure of time series data, see TimeseriesRows.

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