多元索引可以加速Lastpoint索引的資料檢索,並提供多維查詢和統計分析功能。本文介紹在Java SDK中如何通過多元索引來檢索Lastpoint索引資料。
注意事項
Table StoreJava SDK從5.17.1版本開始支援Lastpoint索引功能。使用該功能時,請將SDK版本升級到5.17.1及以上版本。
如果您需要下載新版本的Java SDK,請參見Java SDK歷史迭代版本。
前提條件
已在時序表上建立Lastpoint索引。具體操作,請參見建立Lastpoint索引。
使用流程
1. 建立多元索引
以下樣本用於為Lastpoint索引建立一個多元索引。樣本情境及資料請參見附錄。
樣本中_tags
列為標籤構成的字串數組,建議將對應的多元索引欄位類型設定為Keyword數組,以便更加方便地對_tags
內的標籤進行查詢。
private static void createSearchIndex(SyncClient client) {
CreateSearchIndexRequest createSearchIndexRequest = new CreateSearchIndexRequest();
createSearchIndexRequest.setTableName("<LASTPOINT_INDEX_NAME>");
createSearchIndexRequest.setIndexName("<SEARCH_INDEX_NAME>");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("_#h", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("_m_name", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("_data_source", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("_tags", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true).setIsArray(true),
new FieldSchema("_time", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("gps", FieldType.GEO_POINT).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("speed", FieldType.DOUBLE).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("status", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("total_mileage", FieldType.DOUBLE).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("remaining_mileage", FieldType.DOUBLE).setIndex(true).setEnableSortAndAgg(true)
));
createSearchIndexRequest.setIndexSchema(indexSchema);
client.createSearchIndex(createSearchIndexRequest);
}
2. 通過多元索引檢索資料
此處以範圍查詢為例介紹多元索引查詢功能的使用。
以下樣本用於通過多元索引檢索Lastpoint索引中speed值大於20.0的行資料。
private static void rangeQuery(SyncClient client) {
SearchQuery searchQuery = new SearchQuery();
RangeQuery rangeQuery = new RangeQuery(); //設定查詢類型為RangeQuery。
rangeQuery.setFieldName("speed"); //設定要匹配的欄位。
rangeQuery.greaterThan(ColumnValue.fromDouble(20.0)); //設定該欄位的範圍條件為大於20.0。
searchQuery.setGetTotalCount(true);
searchQuery.setQuery(rangeQuery);
//設定按照speed列逆序排序。
FieldSort fieldSort = new FieldSort("speed");
fieldSort.setOrder(SortOrder.DESC);
searchQuery.setSort(new Sort(Arrays.asList((Sort.Sorter)fieldSort)));
searchQuery.setGetTotalCount(true); //設定返回匹配的總行數。
SearchRequest searchRequest = new SearchRequest("<LASTPOINT_INDEX_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
//通過設定columnsToGet參數可以指定返回的列或返回所有列,如果不設定此參數,則預設只返回主鍵列。
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true); //設定為返回所有列。
searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
System.out.println("TotalCount: " + resp.getTotalCount()); //列印匹配到的總行數,非返回行數。
System.out.println("Row: " + resp.getRows());
}
常見問題
相關文檔
多元索引的核心功能包括任意列的查詢(包括主鍵列和非主鍵列)、多欄位自由組合查詢、地理位置查詢、全文檢索索引、模糊查詢、首碼查詢、巢狀查詢、去重、排序、查詢資料總行數和統計彙總等。更多資訊,請參見多元索引功能。
附錄
在車連網情境中,車輛通過感應器上報時序資料到雲端。通過儲存、查詢和分析這些時序資料,使用者可以實現車況報告、車輛定位、交通管理和軌跡投屏等業務需求。
假設時序表的資料樣本如下:
其中_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 |
Table Store會自動同步時序表中時間軸的最新時間點資料到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 |