Create a time series table
Create a time series table and configure data and metadata time-to-live (TTL), time series identifiers, data field primary keys, analytical stores, and Lastpoint indexes with Tablestore SDK for Java.
Prerequisites
Install the Tablestore SDK for Java and initialize a time series client.
Description
Call createTimeseriesTable to create a time series table that stores time series data and metadata. The time series table name must be unique among data tables and time series tables in the instance.
public CreateTimeseriesTableResponse createTimeseriesTable(CreateTimeseriesTableRequest request)
The following example creates example_timeseries_table. By default, the time series data TTL is -1, which means that the data does not expire, and no analytical store is created.
TimeseriesClient timeseriesClient = client.asTimeseriesClient();
String tableName = "example_timeseries_table";
TimeseriesTableMeta tableMeta = new TimeseriesTableMeta(tableName);
CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(tableMeta);
timeseriesClient.createTimeseriesTable(request);
Parameters
CreateTimeseriesTableRequest contains the following parameters.
|
Name |
Type |
Description |
|
timeseriesTableMeta (required) |
|
The time series table schema. |
|
analyticalStores (optional) |
|
The custom analytical stores. Default value: an empty list. You can create at most one analytical store when you create a time series table. |
|
enableAnalyticalStore (optional) |
|
Specifies whether to create the default analytical store. Default value: |
|
lastpointIndexes (optional) |
|
The Lastpoint indexes to create together with the time series table. Default value: an empty list. You can create at most one Lastpoint index when you create a time series table. A time series table can have up to 10 Lastpoint indexes and analytical stores in total. |
Time series table schema
timeseriesTableMeta is of the TimeseriesTableMeta type and contains the following parameters.
|
Name |
Type |
Description |
|
timeseriesTableName (required) |
|
The time series table name. It must be unique among data tables and time series tables in the instance. |
|
timeseriesTableOptions (optional) |
|
The time series data TTL settings. Default settings are used if this parameter is not configured. |
|
timeseriesMetaOptions (optional) |
|
The time series metadata settings, including metadata TTL and attribute update settings. |
|
timeseriesKeys (optional) |
|
The fields that form a custom time series identifier. You can specify up to six fields. By default, a time series identifier consists of the metric name, data source, and tags. For more information, see Customize time series identifiers and data field primary keys. |
|
fieldPrimaryKeys (optional) |
|
The data fields to use as primary keys. You can specify up to four fields to store multiple rows that have the same time series identifier and timestamp. For more information, see Customize time series identifiers and data field primary keys. |
Time series data retention
timeseriesTableMeta.timeseriesTableOptions is of the TimeseriesTableOptions type and contains the following parameter.
|
Name |
Type |
Description |
|
timeToLive (optional) |
|
The time series data TTL in seconds. Default value: |
Time series metadata
timeseriesTableMeta.timeseriesMetaOptions is of the TimeseriesMetaOptions type and contains the following parameters.
|
Name |
Type |
Description |
|
metaTimeToLive (optional) |
|
The time series metadata TTL in seconds. Set the value to |
|
allowUpdateAttributes (optional) |
|
Specifies whether metadata attribute columns can be updated. If |
Data field primary keys
Each element in timeseriesTableMeta.fieldPrimaryKeys[] is of the PrimaryKeySchema type and contains the following parameters.
|
Name |
Type |
Description |
|
name (required) |
|
The data field name to use as a primary key. |
|
type (required) |
|
The data field type. Valid values: |
Analytical store
Each element in analyticalStores[] is of the TimeseriesAnalyticalStore type and contains the following parameters.
|
Name |
Type |
Description |
|
analyticalStoreName (required) |
|
The analytical store name. |
|
timeToLive (optional) |
|
The analytical store TTL in seconds. Default value: |
|
syncOption (optional) |
|
The data synchronization mode. Default value: |
Lastpoint index
Each element in lastpointIndexes[] is of the CreateTimeseriesTableRequest.LastpointIndex type and contains the following parameter.
|
Name |
Type |
Description |
|
indexName (required) |
|
The Lastpoint index name. For more information, see Lastpoint indexes. |
Examples
Configure the metadata TTL
The following example sets the time series data TTL to 7 days, the metadata TTL to 14 days, and disables metadata attribute updates.
String tableName = "example_timeseries_table";
TimeseriesTableOptions tableOptions = new TimeseriesTableOptions(604800);
TimeseriesTableMeta tableMeta = new TimeseriesTableMeta(tableName, tableOptions);
TimeseriesMetaOptions metaOptions = new TimeseriesMetaOptions();
metaOptions.setMetaTimeToLive(1209600);
metaOptions.setAllowUpdateAttributes(false);
tableMeta.setTimeseriesMetaOptions(metaOptions);
CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(tableMeta);
timeseriesClient.createTimeseriesTable(request);
Customize time series identifiers and data field primary keys
Custom time series identifiers and data field primary keys require Tablestore SDK for Java 5.17.1 or later. We recommend that you use the latest SDK version. Before you use these features, make sure that the region of the instance supports them.
The following example uses pk1 and pk2 as the time series identifier and uses field1 and field2 as data field primary keys.
String tableName = "example_timeseries_table";
TimeseriesTableMeta tableMeta = new TimeseriesTableMeta(tableName);
tableMeta.addTimeseriesKey("pk1");
tableMeta.addTimeseriesKey("pk2");
tableMeta.addFieldPrimaryKey("field1", PrimaryKeyType.INTEGER);
tableMeta.addFieldPrimaryKey("field2", PrimaryKeyType.STRING);
CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(tableMeta);
timeseriesClient.createTimeseriesTable(request);
Create a Lastpoint index
Lastpoint indexes require Tablestore SDK for Java 5.17.1 or later. We recommend that you use the latest SDK version. To use this feature, create a time series model instance in a supported region. For supported regions, see Lastpoint indexes.
The following example creates example_lastpoint_index together with the time series table.
String tableName = "example_timeseries_table";
TimeseriesTableMeta tableMeta = new TimeseriesTableMeta(tableName);
CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(tableMeta);
request.addLastpointIndex(
new CreateTimeseriesTableRequest.LastpointIndex("example_lastpoint_index"));
timeseriesClient.createTimeseriesTable(request);
Create an analytical store
An analytical store provides cost-effective storage and analysis for time series data. To use this feature, create a time series model instance in a supported region. For supported regions and feature details, see Analytical store.
Default analytical store
Set enableAnalyticalStore to true to create the default analytical store named default_analytical_store.
String tableName = "example_timeseries_table";
TimeseriesTableMeta tableMeta = new TimeseriesTableMeta(tableName);
CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(tableMeta);
request.setEnableAnalyticalStore(true);
timeseriesClient.createTimeseriesTable(request);
Custom analytical store
The following example creates example_analytical_store together with the time series table. Data in the analytical store does not expire, and full synchronization is used.
String tableName = "example_timeseries_table";
TimeseriesTableMeta tableMeta = new TimeseriesTableMeta(tableName);
CreateTimeseriesTableRequest request = new CreateTimeseriesTableRequest(tableMeta);
TimeseriesAnalyticalStore analyticalStore =
new TimeseriesAnalyticalStore("example_analytical_store");
analyticalStore.setTimeToLive(-1);
analyticalStore.setSyncOption(AnalyticalStoreSyncType.SYNC_TYPE_FULL);
request.setAnalyticalStores(Collections.singletonList(analyticalStore));
timeseriesClient.createTimeseriesTable(request);
References
-
After you create a time series table, write time series data to the table.
-
After you write data, read time series data from the table.