You can call the CreateTimeseriesTable operation to create a time series table. When you create a time series table, you must specify the configuration information related to the time series table and time series metadata. Additionally, you can specify custom time series identifiers and custom data fields as the primary key columns of the time series table, create an analytical store, and create a Lastpoint index based on your business requirements.
Usage notes
Tablestore SDK for Python V6.1.0 and later support the TimeSeries model. Make sure that you use the correct version of Tablestore SDK for Python.
NoteFor more information, see Version history of Tablestore SDK for Python.
The name of a time series table must be unique within an instance and cannot be the same as an existing table name.
You can create only one analytical store for a time series table. The total number of analytical stores and Lastpoint indexes cannot exceed 10.
NoteThe analytical store and Lastpoint index features are supported in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), and China (Zhangjiakou).
You can specify up to four data fields as the primary key columns of a time series table.
When you specify custom time series identifiers for a time series table, you can add a maximum of six fields
Prerequisites
A Tablestore client is initialized. For more information, see Initialize a Tablestore client.
Parameters
The following table describes the parameters included in request.
Parameter | Description |
table_meta (required) | The schema information of the time series table, which consists of the following items:
|
analytical_stores (optional) | The configuration information of the analytical store for time series, which consists of the following items:
|
lastpoint_index_metas (optional) | The configuration information of the Lastpoint index, which consists of the following item:
|
Examples
Create a time series table
The following sample code provides an example on how to create a time series table:
try:
# Set the retention period of data in the time series table to 172,800 seconds (two days).
tableOption = TimeseriesTableOptions(172800)
# Set the retention period of the time series metadata to -1 and specify that updates to attributes of time series metadata are allowed.
metaOption = TimeseriesMetaOptions(-1, True)
tableMeta = TimeseriesTableMeta("", tableOption, metaOption)
# Call the operation to create the time series table.
request = CreateTimeseriesTableRequest(tableMeta)
otsClient.create_timeseries_table(request)
print("create timeseries table success.")
except Exception as e:
# If an exception is thrown, the time series table fails to be created. Handle the exception.
print("create timeseries table failed. %s" % e)Create a time series table with an analytical store
The following sample code provides an example on how to create a time series table with an analytical store:
try:
# Set the retention period of data in the time series table to 172,800 seconds (two days).
tableOption = TimeseriesTableOptions(172800)
# Set the retention period of the time series metadata to -1 and specify that updates to attributes of time series metadata are allowed.
metaOption = TimeseriesMetaOptions(-1, True)
tableMeta = TimeseriesTableMeta("", tableOption, metaOption)
# Configure an analytical store.
analyticalStore = TimeseriesAnalyticalStore("default_analytical_store", -1, SyncType.SYNC_TYPE_FULL)
# Call the operation to create the time series table.
request = CreateTimeseriesTableRequest(tableMeta, [analyticalStore])
otsClient.create_timeseries_table(request)
print("create timeseries table success.")
except Exception as e:
# If an exception is thrown, the time series table fails to be created. Handle the exception.
print("create timeseries table failed. %s" % e)Create a time series table with custom time series identifiers and custom data fields specified as the primary key columns
The following sample code provides an example on how to create a time series table with custom time series identifiers keyA, keyB, and keyC, and two data fields gid (String type) and uid (Integer type) specified as the primary key columns of the time series table:
try:
# Set the retention period of data in the time series table to 86,400 seconds (one day).
tableOption = TimeseriesTableOptions(86400)
# Set the retention period of the time series metadata to -1 and specify that updates to attributes of time series metadata are allowed.
metaOption = TimeseriesMetaOptions(-1, True)
# Specify custom time series identifiers
timeseriesKeys = ["keyA", "keyB", "keyC"]
# Specify custom data fields as the primary key columns of the time series table.
fieldPrimaryKeys = [('gid', 'STRING'), ('uid', 'INTEGER')]
tableMeta = TimeseriesTableMeta("", tableOption, metaOption, timeseriesKeys,
fieldPrimaryKeys)
# Call the operation to create the time series table.
request = CreateTimeseriesTableRequest(tableMeta)
otsClient.create_timeseries_table(request)
print("create timeseries table success.")
except Exception as e:
# If an exception is thrown, the time series table fails to be created. Handle the exception.
print("create timeseries table failed. %s" % e)Create a time series table with a Lastpoint index
The following sample code provides an example on how to create a time series table with a Lastpoint index:
try:
# Set the retention period of data in the time series table to 86,400 seconds (one day).
tableOption = TimeseriesTableOptions(86400)
# Set the retention period of the time series metadata to 604,800 seconds (seven days) and specify that updates to attributes of time series metadata are not allowed.
metaOption = TimeseriesMetaOptions(604800, False)
tableMeta = TimeseriesTableMeta("", tableOption, metaOption)
# Configure a Lastpoint index.
lastPointIndex = LastpointIndexMeta("")
# Call the operation to create the time series table.
request = CreateTimeseriesTableRequest(tableMeta, None, [lastPointIndex])
otsClient.create_timeseries_table(request)
print("create timeseries table success.")
except Exception as e:
# If an exception is thrown, the time series table fails to be created. Handle the exception.
print("create timeseries table failed. %s" % e)