TablestoreGrid实例是Grid模型的客户端,使用TablestoreGrid实例可以进行创建表、删除表、创建索引表、读写数据等操作。
操作步骤
- 安装Grid模型Java SDK。在Maven项目中使用Grid模型的Java SDK,只需在pom.xml中加入如下依赖。
<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 阿里云账号或者RAM用户的AccessKey ID和AccessKey Secret。具体操作,请参见如何获取AccessKey。 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); }