赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 表格存储
  4. 开发参考
  5. SDK参考
  6. Go SDK
  7. 时序模型
  8. Lastpoint索引
  9. 检索Lastpoint索引

检索Lastpoint索引

  • Lastpoint索引
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

多元索引可以加速Lastpoint索引的数据检索,并提供多维查询和统计分析功能。本文介绍在Go SDK中如何通过多元索引来检索Lastpoint索引数据。

注意事项

表格存储Go SDK从v1.7.15版本开始支持Lastpoint索引功能。使用该功能时,请将SDK版本升级到v1.7.15及以上版本。

前提条件

已在时序表上创建Lastpoint索引。具体操作,请参见创建Lastpoint索引。

使用流程

1. 创建多元索引

以下示例用于为Lastpoint索引创建一个多元索引。示例场景及数据请参见附录。

说明

示例中_tags列为标签构成的字符串数组,建议将对应的多元索引字段类型设置为Keyword数组,以便更加方便地对_tags内的标签进行查询。

func createSearchIndex(client *tablestore.TableStoreClient) {
	request := &tablestore.CreateSearchIndexRequest{}
	request.TableName = "<LASTPOINT_INDEX_NAME>"
	request.IndexName = "<SEARCH_INDEX_NAME>"
	request.IndexSchema = &tablestore.IndexSchema{
		FieldSchemas: []*tablestore.FieldSchema{
			{
				FieldName:        proto.String("_#h"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("_m_name"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("_data_source"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("_tags"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
				IsArray:          proto.Bool(true),
			},
			{
				FieldName:        proto.String("_time"),
				FieldType:        tablestore.FieldType_LONG,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("gps"),
				FieldType:        tablestore.FieldType_GEO_POINT,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("speed"),
				FieldType:        tablestore.FieldType_DOUBLE,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("status"),
				FieldType:        tablestore.FieldType_KEYWORD,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("total_mileage"),
				FieldType:        tablestore.FieldType_LONG,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
			{
				FieldName:        proto.String("remaining_mileage"),
				FieldType:        tablestore.FieldType_LONG,
				Index:            proto.Bool(true),
				EnableSortAndAgg: proto.Bool(true),
			},
		},
	}
	_, err := client.CreateSearchIndex(request)
	if err != nil {
		fmt.Println("Failed to create searchIndex with error:", err)
		return
	}
}

2. 通过多元索引检索数据

此处以范围查询为例介绍多元索引查询功能的使用。

以下示例用于通过多元索引检索Lastpoint索引中speed值大于20.0的行数据。

func RangeQuery(client *tablestore.TableStoreClient, lastpointName string, indexName string) {
	searchRequest := &tablestore.SearchRequest{}
	searchRequest.SetTableName(lastpointName)
	searchRequest.SetIndexName(indexName)
	searchQuery := search.NewSearchQuery()
	rangeQuery := &search.RangeQuery{} //设置查询类型为RangeQuery。
	rangeQuery.FieldName = "speed" //设置要匹配的字段
	rangeQuery.GT(20.0)                //设置该字段的范围条件为大于20.0。
	searchQuery.SetQuery(rangeQuery)
	//设置按照speed列逆序排序。
	searchQuery.SetSort(&search.Sort{
		[]search.Sorter{
			&search.FieldSort{
				FieldName: "speed",
				Order:     search.SortOrder_DESC.Enum(),
			},
		},
	})
	searchRequest.SetSearchQuery(searchQuery)
	searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{
		ReturnAll: true,
	})
	searchResponse, err := client.Search(searchRequest)
	if err != nil {
		fmt.Printf("%#v", err)
		return
	}
	fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) //查看返回结果是否完整。
	fmt.Println("RowCount: ", len(searchResponse.Rows))
	for _, row := range searchResponse.Rows {
		jsonBody, err := json.Marshal(row)
		if err != nil {
			panic(err)
		}
		fmt.Println("Row: ", string(jsonBody))
	}
}

常见问题

  • 使用多元索引Search接口查不到数据

  • 如何将多元索引 Search 接口查询数据的 limit 提高到 1000

  • 为什么使用多元索引翻页查询时Token失效了?

相关文档

多元索引的核心功能包括任意列的查询(包括主键列和非主键列)、多字段自由组合查询、地理位置查询、全文检索、模糊查询、前缀查询、嵌套查询、去重、排序、查询数据总行数和统计聚合等。更多信息,请参见多元索引功能。

附录

在车联网场景中,车辆通过传感器上报时序数据到云端。通过存储、查询和分析这些时序数据,用户可以实现车况报告、车辆定位、交通管理和轨迹投屏等业务需求。

假设时序表的数据示例如下:

说明

其中_m_name、_data_source和_tags为时间线标识,分别代表度量名称、数据源和时间线的标签信息,_time为数据上报时间。gps、speed、status、total_mileage和remaining_mileage为时间线的时序数据,分别代表车辆GPS坐标、车辆速度、车辆状态、车辆总里程和车辆剩余里程。

_m_name

_data_source

_tags

_time

gps

speed

status

total_mileage

remaining_mileage

平台A

sensor1

["region=hangzhou","car_model=sedan","number_plate=浙AD7512*","color=white"]

1730422800000000

30.245853,120.178564

0

闲置

20000

450

平台A

sensor1

["region=hangzhou","car_model=sedan","number_plate=浙AD7512*","color=white"]

1730423400000000

30.245853,120.178564

0

闲置

20000

450

平台A

sensor2

["region=hangzhou","car_model=suv","number_plate=浙C72B2*","color=black"]

1730779200000000

30.245278,120.150269

50

使用中

15000

300

平台A

sensor2

["region=hangzhou","car_model=suv","number_plate=浙C72B2*","color=black"]

1730779800000000

30.245853,120.213654

80

使用中

15050

250

平台B

sensor3

["region=hangzhou","car_model=sedan","number_plate=浙B121*9","color=blue"]

1730862000000000

30.246013,120.124470

60

使用中

18200

300

平台B

sensor3

["region=hangzhou","car_model=sedan","number_plate=浙B121*9","color=blue"]

1730862600000000

30.246022,120.124460

0

闲置

18230

270

表格存储会自动同步时序表中时间线的最新时间点数据到Lastpoint索引表,Lastpoint索引中的数据示例如下:

_#h

_m_name

_data_source

_tags

_time

gps

speed

status

total_mileage

remaining_mileage

4c#平台A#07

平台A

sensor1

["region=hangzhou","car_model=sedan","number_plate=浙AD7512*","color=white"]

1730423400000000

30.245853,120.178564

0

闲置

20000

450

25#平台A#ae

平台A

sensor2

["region=hangzhou","car_model=suv","number_plate=浙C72B2*","color=black"]

1730779800000000

30.245853,120.213654

80

使用中

15050

250

b2#平台B#4b

平台B

sensor3

["region=hangzhou","car_model=sedan","number_plate=浙B121*9","color=blue"]

1730862600000000

30.246022,120.124460

0

闲置

18230

270

相关文章

创建Lastpoint索引 2025-04-22 14:24

Lastpoint索引可用于快速检索各时间线的最新时间点数据,您可以使用CreateTimeseriesLastpointIndex接口为时序表创建Lastpoint索引。创建Lastpoint索引时,您需要配置时序表名称和Lastpoint索引名称,以及根据需要配置是否在Lastpoint索引中包

查询Lastpoint索引数据 2025-04-22 14:24

本文介绍如何通过Go SDK查询Lastpoint索引数据。 注意事项 表格存储Go SDK从v1.7.15版本开始支持Lastpoint索引功能。使用该功能时,请将SDK版本升级到v1.7.15及以上版本。 前提条件

删除Lastpoint索引 2025-04-22 14:24

当不再需要使用Lastpoint索引获取时序表中各时间线的最新时间点时,您可以使用DeleteTimeseriesLastpointIndex接口删除Lastpoint索引。 注意事项 表格存

检索Lastpoint索引 2025-04-22 14:24

多元索引可以加速Lastpoint索引的数据检索,并提供多维查询和统计分析功能。本文介绍在Go SDK中如何通过多元索引来检索Lastpoint索引数据。 注意事项 表格存储Go SDK从v1.7.15版本开始支持Lastpoint索引功能。使用该功能时,请将SDK版本升级到v1.7.15及以上版本

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号