TablestoreGrid实例是Grid模型的客户端,您可以使用实例进行建表、删表、创建索引表以及读写数据等操作。
操作步骤
- 安装Grid模型Java SDK
在使用Grid模型前,首先要在代码中安装Grid模型的SDK,Maven依赖如下:
<dependency> <groupId>com.aliyun.tablestore</groupId> <artifactId>tablestore-grid</artifactId> <version>1.0.0</version> </dependency>
- 创建TablestoreGrid实例
TableStoreGrid实例是Grid模型的客户端,用于执行Grid模型的各种操作。
- 示例代码
private TableStoreGrid createTableStoreGrid() { TableStoreGridConfig config = new TableStoreGridConfig(); config.setTableStoreEndpoint("TABLESTORE_ENDPOINT"); config.setAccessId("ACCESS_ID"); config.setAccessKey("ACCESS_KEY"); config.setTableStoreInstance("TABLESTORE_INSTANCE_NAME"); config.setDataTableName("GRID_DATA_TABLE"); config.setMetaTableName("GRID_META_TABLE"); TableStoreGrid tableStoreGrid = new TableStoreGrid(config); return tableStoreGrid; }
- 参数说明
参数 说明 TABLESTORE_ENDPOINT 表格存储实例的服务地址(Endpoint),具体参见服务地址。 ACCESS_ID和ACCESS_KEY 云账号的访问鉴权信息。关于如何获取AccessKey Id和AccessKey Secret,参见查看访问密钥基本信息。 TABLESTORE_INSTANCE_NAME 表格存储实例名。 GRID_DATA_TABLE 存放网格数据的表名,表名建议具有实际业务意义。 说明 此步仅配置数据表的表名,不会创建该数据表。GRID_META_TABLE 存放网格元数据的表名,表名建议具有实际业务意义。 说明 此步仅配置数据表的表名,不会创建该数据表。
- 示例代码
- 创建表
上步中配置了数据表和元数据表的表名,实际表还未创建,您需要通过createStore接口创建表,示例代码如下:
private void createTable(TableStoreGrid grid) throws Exception { grid.createStore(); }
- 创建元数据索引
元数据索引用于对网格元数据进行检索,索引信息中需要包含需要被检索的元数据列等信息。
下面示例中对status、tag1、tag2、create_time这4列创建了索引,您可以自己设计元数据中需要包含的列,以及需要创建索引的列。private void createIndex(TableStoreGrid grid) throws Exception { IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("status", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true), new FieldSchema("tag1", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true), new FieldSchema("tag2", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true), new FieldSchema("create_time", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true) )); grid.createMetaIndex("GRID_META_INDEX_NAME", indexSchema); }
在文档使用中是否遇到以下问题
更多建议
匿名提交