全部產品
Search
文件中心

Tablestore:建立時序表

更新時間:Apr 26, 2025

當使用Table Store時序模型時,您需要使用CreateTimeseriesTable介面建立時序表用於儲存時序資料。建立時序表時支援配置資料生命週期、時序時間軸表的配置資訊、自訂時間軸標識和作為主鍵的資料欄位、建立Lastpoint索引以及建立分析儲存。分析儲存可用於快速分析時序資料。Lastpoint索引可用於快速檢索各時間軸的最新時間點的資料。

前提條件

注意事項

  • 時序表的名稱不能與當前已存在的資料表名稱相同。

    說明

    您可以使用ListTimeseriesTable介面列出指定執行個體下已有的時序表名稱。具體操作,請參見列出時序表名稱

  • 一個時序表只能建立一個分析儲存。

  • 一個時序表的Lastpoint索引與分析儲存的數量總和不得超過10個。

  • 一個時序表最多隻能添加4個作為主鍵的資料欄位。

  • 為一個時序表自訂時間軸標識時,最多隻能添加6個欄位。

參數

參數

說明

TimeseriesTableMeta

時序表的結構資訊,包含如下內容:

  • timeseriesTableName:時序表名。

  • timeseriesMetaOptions:時序時間軸表的配置資訊,包括如下內容:

    • metaTimeToLive:配置時序時間軸表的時間軸資料存活時間,單位為秒。取值必須大於等於604800秒(即7天)或者必須為-1(資料永不到期)。

    • allowUpdateAttributes:是否允許更新時間軸屬性列。

    您可以通過UpdateTimeseriesTable介面修改相應配置。

  • timeseriesTableOptions:時序表的配置資訊,包括如下內容:

    timeToLive:配置時序表的資料存活時間,單位為秒。如果希望資料永不到期,可以設定為-1。您可以通過UpdateTimeseriesTable介面修改。

  • timeseriesKeys:自訂時間軸標識資訊,預設使用度量名稱、資料來源和標籤構成時間軸標識。更多資訊,請參見自訂時間軸標識和作為主鍵的資料欄位。只有當要自訂時間軸標識時,才需要設定此參數。

  • fieldPrimaryKeys:作為主鍵的資料欄位,可用於實現在時序表中儲存多行時間軸標識和時間點相同的資料。更多資訊,請參見自訂時間軸標識和作為主鍵的資料欄位。只有當要同時儲存時間軸標識相同的多行資料時,才需要設定此參數。

  • lastpointIndexes:Lastpoint索引名稱。關於Lastpoint索引的更多資訊,請參見Lastpoint索引

enableAnalyticalStore

是否建立預設分析儲存。預設值為true,表示建立預設分析儲存。

建立分析儲存後,您可以流量分析儲存低成本儲存時序資料以及查詢與分析時序資料。更多資訊,請參見分析儲存

如果不需要建立分析儲存,請將此參數設定為false。

analyticalStores

分析儲存配置,包括如下內容:

  • analyticalStoreName:分析儲存名稱。

  • timeToLive:分析儲存的資料生命週期。單位為秒。取值範圍為-1(資料永不到期)或者大於等於2592000秒(即30天)的int32的正整數。

    如果希望資料永不到期,可以設定為-1。

  • syncOption:資料同步配置。固定取值為SYNC_TYPE_FULL。

樣本

建立不帶分析儲存的時序表

以下樣本用於建立一個時序表。

/**
 * CreateTimeseriesTableSample用於建立一個時序表,時序表名為timeseriesTableName,TTL為timeTolive。
 */
func CreateTimeseriesTableSample(client *tablestore.TimeseriesClient, timeseriesTableName string , timeToLive int64) {
    fmt.Println("[Info]: Begin to create timeseries table: " , timeseriesTableName)

    // 構造時序表配置資訊。
    timeseriesTableOptions := tablestore.NewTimeseriesTableOptions(timeToLive) 

    // 構造表中繼資料資訊。
    // 設定時序表名。
    timeseriesTableMeta := tablestore.NewTimeseriesTableMeta(timeseriesTableName) 
    // 設定時序表配置資訊。
    timeseriesTableMeta.SetTimeseriesTableOptions(timeseriesTableOptions)    

    // 構造建立時序表請求。
    createTimeseriesTableRequest := tablestore.NewCreateTimeseriesTableRequest()   
    createTimeseriesTableRequest.SetTimeseriesTableMeta(timeseriesTableMeta)
    // 不建立預設分析儲存。
    createTimeseriesTableRequest.SetEnableAnalyticalStore(false)  

    // 調用client建立時序表。
    createTimeseriesTableResponse , err := client.CreateTimeseriesTable(createTimeseriesTableRequest) 
    if err != nil {
        fmt.Println("[Error]: Failed to create timeseries table with error: " , err)
        return
    }
    fmt.Println("[Info]: CreateTimeseriesTable finished! RequestId: " , createTimeseriesTableResponse.RequestId)
}

建立時序表時建立分析儲存

建立時序表時支援建立預設分析儲存或者建立自訂分析儲存。

建立時序表時建立預設分析儲存

以下樣本用於建立時序表的同時建立一個預設分析儲存。其中預設分析儲存的名稱固定為default_analytical_store,且預設分析儲存內的資料永不到期。

/**
 * CreateTimeseriesTableSample用於建立一個時序表,時序表名為timeseriesTableName,TTL為timeTolive。
 */
func CreateTimeseriesTableSample(client *tablestore.TimeseriesClient, timeseriesTableName string , timeToLive int64) {
    fmt.Println("[Info]: Begin to create timeseries table: " , timeseriesTableName)

    // 構造時序表配置資訊。
    timeseriesTableOptions := tablestore.NewTimeseriesTableOptions(timeToLive)        

    // 構造表中繼資料資訊。
    // 設定時序表名。
    timeseriesTableMeta := tablestore.NewTimeseriesTableMeta(timeseriesTableName)  
    // 設定時序表配置資訊。
    timeseriesTableMeta.SetTimeseriesTableOptions(timeseriesTableOptions)      

    // 構造建立時序表請求。
    createTimeseriesTableRequest := tablestore.NewCreateTimeseriesTableRequest()    
    createTimeseriesTableRequest.SetTimeseriesTableMeta(timeseriesTableMeta)

    // 調用client建立時序表。
    createTimeseriesTableResponse , err := client.CreateTimeseriesTable(createTimeseriesTableRequest)    
    if err != nil {
        fmt.Println("[Error]: Failed to create timeseries table with error: " , err)
        return
    }
    fmt.Println("[Info]: CreateTimeseriesTable finished! RequestId: " , createTimeseriesTableResponse.RequestId)
}

建立時序表時建立自訂分析儲存

以下樣本用於建立時序表的同時建立一個自訂分析儲存。該自訂分析儲存的名稱為test_analytical_store,且自訂分析儲存內的資料永不到期。

/**
 * CreateTimeseriesTableSample用於建立一個時序表,時序表名為timeseriesTableName,TTL為timeTolive,並且建立一個分析儲存test_analytical_store。
 */
func CreateTimeseriesTableSample(client *tablestore.TimeseriesClient, timeseriesTableName string , timeToLive int64) {
    fmt.Println("[Info]: Begin to create timeseries table: " , timeseriesTableName)

    // 構造時序表配置資訊。
    timeseriesTableOptions := tablestore.NewTimeseriesTableOptions(timeToLive)    

    // 構造表中繼資料資訊。
    // 設定時序表名。
    timeseriesTableMeta := tablestore.NewTimeseriesTableMeta(timeseriesTableName)  
    // 設定時序表配置資訊。
    timeseriesTableMeta.SetTimeseriesTableOptions(timeseriesTableOptions)  

    // 構造建立時序表請求。
    createTimeseriesTableRequest := tablestore.NewCreateTimeseriesTableRequest() 
    createTimeseriesTableRequest.SetTimeseriesTableMeta(timeseriesTableMeta)

    // 構造分析儲存配置資訊。
    createTimeseriesTableRequest.SetAnalyticalStores([]*tablestore.TimeseriesAnalyticalStore{
        tablestore.NewTimeseriesAnalyticalStore("test_analytical_store"),
    })

    // 調用client建立時序表。
    createTimeseriesTableResponse , err := client.CreateTimeseriesTable(createTimeseriesTableRequest) 
    if err != nil {
        fmt.Println("[Error]: Failed to create timeseries table with error: " , err)
        return
    }
    fmt.Println("[Info]: CreateTimeseriesTable finished! RequestId: " , createTimeseriesTableResponse.RequestId)
}

建立時序表時自訂時間軸標識和作為主鍵的資料欄位

重要

Table StoreGo SDK最新版本支援自訂時間軸標識和作為主鍵的資料欄位功能。使用該功能時,請確保擷取了正確的Go SDK版本。

以下樣本建立一張時序表,時間軸標識由pk1和pk2構成,同時有三個作為主鍵的資料欄位field1、field2和field3,欄位類型分別為整型、字串和二進位。

/**
 * CreateTimeseriesTableSample用於建立一個時序表,時序表名為timeseriesTableName,TTL為timeTolive。
 */
func CreateTimeseriesTableSample(client *tablestore.TimeseriesClient, timeseriesTableName string, timeToLive int64) {
	fmt.Println("[Info]: Begin to create timeseries table: ", timeseriesTableName)

	timeseriesTableOptions := tablestore.NewTimeseriesTableOptions(timeToLive)
	timeseriesTableMeta := tablestore.NewTimeseriesTableMeta(timeseriesTableName)
	timeseriesTableMeta.SetTimeseriesTableOptions(timeseriesTableOptions)
	// 添加自訂時間軸標識。
	timeseriesTableMeta.AddTimeseriesKey("pk1")
	timeseriesTableMeta.AddTimeseriesKey("pk2")
	// 添加作為主鍵的資料欄位。
	timeseriesTableMeta.AddFieldPrimaryKey("field1", tablestore.PrimaryKeyType_INTEGER)
	timeseriesTableMeta.AddFieldPrimaryKey("field1", tablestore.PrimaryKeyType_STRING)
	timeseriesTableMeta.AddFieldPrimaryKey("field3", tablestore.PrimaryKeyType_BINARY)
	// 構造建立時序表請求。
	createTimeseriesTableRequest := tablestore.NewCreateTimeseriesTableRequest()
	createTimeseriesTableRequest.SetTimeseriesTableMeta(timeseriesTableMeta)

	createTimeseriesTableResponse, err := client.CreateTimeseriesTable(createTimeseriesTableRequest)
	if err != nil {
		fmt.Println("[Error]: Failed to create timeseries table with error: ", err)
		return
	}
	fmt.Println("[Info]: CreateTimeseriesTable finished! RequestId: ", createTimeseriesTableResponse.RequestId)
}

建立時序表時配置Lastpoint索引

重要

Table StoreGo SDK最新版本支援Lastpoint索引功能。使用該功能時,請確保擷取了正確的Go SDK版本。

以下樣本用於在建立一張時序表時同時建立名稱為index1的Lastpoint索引。

func createTimeseriesTable(client *tablestore.TimeseriesClient) {
	timeseriesTableMeta := tablestore.NewTimeseriesTableMeta("test_timeseries_table")
	timeseriesTableMeta.SetTimeseriesTableOptions(tablestore.NewTimeseriesTableOptions(-1))
	request := tablestore.NewCreateTimeseriesTableRequest()
	request.SetTimeseriesTableMeta(timeseriesTableMeta)
	request.SetLastpointIndexNames([]string{"index1"})
	_, err := client.CreateTimeseriesTable(request)
	if err != nil {
		log.Fatal(err)
	}
}

相關文檔

  • 建立時序表後,您可以寫入時序資料以及讀取表中時序資料。具體操作,請參見寫入時序資料查詢時序資料

  • 如果要低成本儲存時序資料以及快速查詢和分析時序資料,您可以為時序表建立分析儲存。具體操作,請參見建立分析儲存

  • 如果要修改時序表的資料生命週期,您可以通過更新時序表功能實現。具體操作,請參見更新時序表

  • 如果要查詢當前執行個體下的所有時序表,您可以通過列出時序表名稱實現。具體操作,請參見列出時序表名稱

  • 如果要查看時序表的詳細配置資訊,您可以通過查詢時序表描述資訊實現。更多資訊,請參見查詢時序表描述資訊

  • 如果不再使用時序表,您可以刪除時序表。具體操作,請參見刪除時序表

  • 如果要以更低成本備份Table Store中的時序資料或者以檔案形式匯出時序資料到本地,您可以通過DataWorksData Integration服務將Table Store中的時序資料匯出到OSS後進行儲存或者下載。更多資訊,請參見將Table Store資料同步到OSS

  • 如果要可視化展示時序資料,您可以通過對接Grafana實現。更多資訊,請參見對接Grafana

  • 通過Flink計算與分析資料後,您可以使用Tablestore時序表格儲存體輸出結果。更多資訊,請參見使用教程(時序模型)