动态修改Schema

更新时间:
复制为 MD 格式

当索引数量较多时需批量修改索引,可通过Java SDK先重建出灰度索引再通过表格存储控制台继续后续操作。

注意事项

由于修改schema时系统会将索引中的所有历史数据进行重建,因此一次最多支持重建5个索引。请等5个索引均重建结束(即在控制台中新旧索引的数据量几乎一致或者走完切换流程)再操作下一批。

操作步骤

步骤一:通过SDK重建索引

// 本示例展示如何创建灰度索引(重建索引)来动态修改多元索引的 Schema
// 使用前请配置环境变量 TABLESTORE_ACCESS_KEY_ID 和 TABLESTORE_ACCESS_KEY_SECRET

import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.core.ResourceManager;
import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentialProvider;
import com.alicloud.openservices.tablestore.core.auth.DefaultCredentials;
import com.alicloud.openservices.tablestore.core.auth.V4Credentials;
import com.alicloud.openservices.tablestore.model.search.CreateSearchIndexRequest;
import com.alicloud.openservices.tablestore.model.search.CreateSearchIndexResponse;
import com.alicloud.openservices.tablestore.model.search.FieldSchema;
import com.alicloud.openservices.tablestore.model.search.FieldType;
import com.alicloud.openservices.tablestore.model.search.IndexSchema;
import com.alicloud.openservices.tablestore.model.search.IndexSetting;

import java.util.Arrays;

public class CreateReindex {

    public static void main(String[] args) {
        String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

        // 实例配置
        String region = "<region-id>";
        String instanceName = "example-instance";
        String endpoint = "https://example-instance.<region-id>.ots.aliyuncs.com";

        // 表名和索引名
        String tableName = "example_table";
        String sourceIndexName = "example_index";
        // 灰度索引名必须以 _reindex 结尾
        String reindexName = sourceIndexName + "_reindex";

        SyncClient client = null;
        try {
            DefaultCredentials credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
            V4Credentials credentialsV4 = V4Credentials.createByServiceCredentials(credentials, region);
            CredentialsProvider provider = new DefaultCredentialProvider(credentialsV4);
            client = new SyncClient(endpoint, provider, instanceName, null, new ResourceManager(null, null));

            // 创建灰度索引请求
            CreateSearchIndexRequest request = new CreateSearchIndexRequest();
            request.setTableName(tableName);
            request.setIndexName(reindexName);
            // 指定源索引,表示这是基于源索引创建的灰度索引
            request.setSourceIndexName(sourceIndexName);

            // 定义新的索引 Schema(可以新增、修改或删除字段)
            IndexSchema indexSchema = new IndexSchema();
            indexSchema.setFieldSchemas(Arrays.asList(
                    new FieldSchema("pk", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
                    new FieldSchema("col1", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true),
                    // 新增 col3 字段(原索引只有 pk, col1, col2)
                    new FieldSchema("col3", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true)
            ));

            // 设置路由键(用于优化查询性能)
            IndexSetting indexSetting = new IndexSetting();
            indexSetting.setRoutingFields(Arrays.asList("pk"));
            indexSchema.setIndexSetting(indexSetting);

            request.setIndexSchema(indexSchema);

            // 创建灰度索引
            CreateSearchIndexResponse response = client.createSearchIndex(request);
            System.out.println("灰度索引创建成功: " + reindexName);
            System.out.println("RequestId: " + response.getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                client.shutdown();
            }
        }
    }
}

步骤二:通过控制台操作索引

重建索引后,可在表格存储控制台执行后续索引操作,包括查看索引同步进度、设置权重进行 A/B 测试、交换索引Schema以及删除灰度索引。具体操作,请参见动态修改schema