您可以使用Java SDK、Go SDK、Python SDK、Node.js SDK、.NET SDK和PHP SDK在建立多元索引時配置路由鍵。此處以Java SDK為例介紹路由鍵的配置和使用。
以下樣本用於建立order資料表時建立order_index多元索引並指定路由欄位為user_id欄位。然後寫入資料並帶上路由欄位進行查詢。該資料表有order_id(string類型)和user_id(string類型)兩個主鍵;該多元索引有product_name(keyword類型)、order_time(long類型)和user_id(keyword類型)欄位,多元索引的路由鍵為user_id欄位。
private static void testRoute(SyncClient client) throws InterruptedException {
//建立表。
TableMeta meta = new TableMeta("order");
meta.addPrimaryKeyColumn("order_id",PrimaryKeyType.STRING);
meta.addPrimaryKeyColumn("user_id",PrimaryKeyType.STRING);
TableOptions options = new TableOptions();
options.setMaxVersions(1);
options.setTimeToLive(-1);
CreateTableRequest request = new CreateTableRequest(meta,options);
request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0)));
CreateTableResponse response = client.createTable(request);
//建立多元索引並指定路由欄位。
CreateSearchIndexRequest searchIndexRequest = new CreateSearchIndexRequest();
//訂單表。
searchIndexRequest.setTableName("order");
//訂單表索引名。
searchIndexRequest.setIndexName("order_index");
IndexSchema indexSchema = new IndexSchema();
IndexSetting indexSetting = new IndexSetting();
//設定user_id為路由欄位。
indexSetting.setRoutingFields(Arrays.asList("user_id"));
indexSchema.setIndexSetting(indexSetting);
//添加索引欄位。此處只是給出樣本,您可以根據業務需求添加索引欄位。
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("product_name",FieldType.KEYWORD).setStore(true).setIndex(true),
new FieldSchema("order_time",FieldType.LONG).setStore(true).setEnableSortAndAgg(true).setIndex(true),
new FieldSchema("user_id",FieldType.KEYWORD).setStore(true).setIndex(true)
));
searchIndexRequest.setIndexSchema(indexSchema);
client.createSearchIndex(searchIndexRequest);
//等待資料表載入。
Thread.sleep(6*1000);
//插入一些測試資料。
String[] productName = new String[]{"product a", "product b", "product c"};
String[] userId = new String[]{"00001", "00002", "00003", "00004", "00005"};
for (int i = 0; i < 100; i++){
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("order_id",PrimaryKeyValue.fromString(i+""));
primaryKeyBuilder.addPrimaryKeyColumn("user_id",PrimaryKeyValue.fromString(userId[i%(userId.length)]));
PrimaryKey primaryKey = primaryKeyBuilder.build();
RowPutChange rowPutChange = new RowPutChange("order",primaryKey);
//寫入屬性列。
rowPutChange.addColumn("product_name",ColumnValue.fromString(productName[i%(productName.length)]));
rowPutChange.addColumn("order_time",ColumnValue.fromLong(System.currentTimeMillis()));
rowPutChange.setCondition(new Condition(RowExistenceExpectation.IGNORE));
client.putRow(new PutRowRequest(rowPutChange));
}
//等待資料同步到多元索引。
Thread.sleep(20*1000);
//帶上路由欄位的查詢。
SearchRequest searchRequest = new SearchRequest();
searchRequest.setTableName("order");
searchRequest.setIndexName("order_index");
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("user_id");
matchQuery.setText("00002");
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(matchQuery);
searchQuery.setGetTotalCount(true);
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true);
searchRequest.setColumnsToGet(columnsToGet);
searchRequest.setSearchQuery(searchQuery);
PrimaryKey routingValue1 = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("user_id", PrimaryKeyValue.fromString("00001"))
.build();
PrimaryKey routingValue2 = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("user_id", PrimaryKeyValue.fromString("00002"))
.build();
// 如果需要指定多個路由值,可以添加n個routingValue,這樣服務端會從每個routingValue對應的分區裡查詢資料
searchRequest.setRoutingValues(Arrays.asList(routingValue1, routingValue2));
SearchResponse searchResponse = client.search(searchRequest);
System.out.println(searchResponse.isAllSuccess());
System.out.println("totalCount:"+ searchResponse.getTotalCount());
System.out.println("RowCount:"+searchResponse.getRows().size());
}