检索Lastpoint索引

更新时间:2024-12-19 02:52:30

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

注意事项

表格存储Go SDKv1.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))
	}
}

常见问题

相关文档

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

附录

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

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

说明

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

_m_name

_data_source

_tags

_time

gps

speed

status

total_mileage

remaining_mileage

_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

_#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

  • 本页导读 (1)
  • 注意事项
  • 前提条件
  • 使用流程
  • 1. 创建多元索引
  • 2. 通过多元索引检索数据
  • 常见问题
  • 相关文档
  • 附录
AI助理

点击开启售前

在线咨询服务

你好,我是AI助理

可以解答问题、推荐解决方案等