Table 操作
更新时间:2025-01-14
创建表
功能介绍
在指定的库中新建一个表。
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4import com.baidu.mochow.model.CreateTableRequest;
5import com.baidu.mochow.model.entity.Field;
6import com.baidu.mochow.model.entity.HNSWParams;
7import com.baidu.mochow.model.entity.PartitionParams;
8import com.baidu.mochow.model.entity.Schema;
9import com.baidu.mochow.model.entity.SecondaryIndex;
10import com.baidu.mochow.model.entity.VectorIndex;
11import com.baidu.mochow.model.enums.FieldType;
12import com.baidu.mochow.model.enums.IndexType;
13import com.baidu.mochow.model.enums.MetricType;
14import com.baidu.mochow.model.enums.PartitionType;
15public class Main {
16 public static void main(String[] args) {
17 String account = "root";
18 String apiKey = "*********";
19 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
20 ClientConfiguration clientConfiguration = new ClientConfiguration();
21 clientConfiguration.setCredentials(new Credentials(account, apiKey));
22 clientConfiguration.setEndpoint(endpoint);
23 MochowClient mochowClient = new MochowClient(clientConfiguration);
24 String databaseName = "test";
25 String tableName = "test";
26 // create table
27 Schema tableSchema = Schema.builder()
28 .addField(
29 Field.builder()
30 .fieldName("id")
31 .fieldType(FieldType.STRING)
32 .primaryKey(true)
33 .partitionKey(true)
34 .autoIncrement(false)
35 .notNull(true).build())
36 .addField(
37 Field.builder()
38 .fieldName("bookName")
39 .fieldType(FieldType.STRING)
40 .notNull(true).build())
41 .addField(
42 Field.builder()
43 .fieldName("author")
44 .fieldType(FieldType.STRING).build())
45 .addField(
46 Field.builder()
47 .fieldName("page")
48 .fieldType(FieldType.UINT32).build())
49 .addField(
50 Field.builder()
51 .fieldName("segment")
52 .fieldType(FieldType.STRING).build())
53 .addField(
54 Field.builder()
55 .fieldName("vector")
56 .fieldType(FieldType.FLOAT_VECTOR)
57 .dimension(4).build())
58 .addIndex(
59 VectorIndex.builder()
60 .indexName("vector_idx")
61 .indexType(IndexType.HNSW)
62 .fieldName("vector")
63 .params(new HNSWParams(32, 200))
64 .metricType(MetricType.L2)
65 .autoBuild(false).build())
66 .addIndex(new SecondaryIndex("book_name_idx", "bookName"))
67 .build();
68 CreateTableRequest createTableRequest = CreateTableRequest.builder()
69 .database(databaseName)
70 .table(tableName)
71 .replication(3)
72 .partition(new PartitionParams(PartitionType.HASH, 1))
73 .description("test")
74 .schema(tableSchema).build();
75 mochowClient.createTable(createTableRequest);
76 }
77}
相关类说明
CreateTableRequest
创建索引请求,用于向量数据库创建索引,拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明
Java
1import com.baidu.mochow.model.CreateTableRequest; //导入包
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
database | String | 是 | 指定库的名称,在对应的库下创建表 |
table | String | 是 | 指定表的名称。表的命名要求如下: 1. 仅支持大小写字母、数字以及下划线(_),且必须以字母开头; 2. 长度限制为1~255。 |
replication | Int | 是 | 单个分区的总副本数(含主副本),取值范围为[1,10]。 若需要完整的高可用特性,副本总数需>=3。 需要注意的是:总副本数需要小于等于数据节点的数量,否则无法正常建表。 |
partition | PartitionParams | 是 | 表的分区数量,取值范围为[1, 1000]。 若非FLAT索引,则建议将单个分区的记录总数控制在100万到1000万之间,过大过小都不太合适。 |
schema | Schema | 是 | 表的Schema信息。 |
enableDynamicDield | Boolean | 否 | 表是否支持自动增加字段,默认值为False。 |
description | String | 否 | 表的描述信息。 |
PartitionParams
分区参数,拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明
Java
1import com.baidu.mochow.model.PartitionParams; //导入包
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
partitionType | PartitionType | 是 | 分区类型 |
partitionNum | int | 是 | 分区数量 |
Schema
分区参数,拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明
Java
1import com.baidu.mochow.model.PartitionParams; //导入包
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
fields | List<Field> | 是 | 指定表的字段详情列表 |
indexes | List<IndexField> | 是 | 表的索引详情列表 |
Field
参数 | 参数类型 | 是否必选 | 参数含义 |
---|---|---|---|
fieldName | String | 是 | 字段名称。 |
fieldType | FieldType | 是 | 字段数据类型。当前支持如下类型:BOOL、INT8、UINT8、INT16、UINT16、INT32、UINT32、INT64、UINT64、FLOAT、DOUBLE、DATE、DATETIME、TIMESTAMP、STRING、BINARY、UUID、TEXT、TEXT_GBK、TEXT_GB18030、ARRAY和FLOAT_VECTOR。 各数据类型的详细定义和约束请参见数据类型。 |
primaryKey | Boolean | 否 | 是否为主键,默认值为False。 当前已支持多主键,详情参见多主键。 主键字段不支持如下类型:BOOL、FLOAT、DOUBLE和FLOAT_VECTOR。 |
partitionKey | Boolean | 否 | 是否为分区键,默认值为False。 当前仅支持单一字段作为分区键,分区键可以是主键,也可以不是主键,但一张表只能有一个分区键,每行记录都会根据分区键的取值哈希映射到不同的分区。 分区键字段不支持如下类型:BOOL、FLOAT、DOUBLE、ARRAY和FLOAT_VECTOR。 |
autoIncrement | Boolean | 否 | 是否自增主键,默认值为False。 仅适用于类型为UINT64的主键字段,非主键字段请勿填写属性值。 |
notNull | Boolean | 否 | 是否非空,默认值为False。 不可以为空值的字段包括:主键字段、分区键字段、向量字段和索引键字段。 |
dimension | Int | 否 | 向量维度。仅当字段类型为FLOAT_VECTOR时,才需要指定该参数。 |
IndexField
拥有constructor、getter、setter以及builder等通用方法,通用方法详情可以参考通用说明。为VectorIndex和SecondaryIndex的基类,正常情况下不使用
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
indexName | String | 是 | 索引的名字 |
field | String | 是 | 索引作用于的字段名称 |
indexType | IndexType | 是 | 插入的记录列表 |
metricType | MetricType | 否 | 向量之间距离度量算法类型。取值如下: 注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确 |
params | IndexParams | 否 | 向量索引构建参数 |
autoBuild | Boolean | 否 | 是否有自动构建索引策略 |
autoBuildPolicy | AutoBuildPolicy | 否 | 自动构建索引策略参数 periodical,周期性构建索引。 rowCountIncrement,根据tablet行增长数自动构建索引。 timing,定时构建索引 |
state | IndexState | 否 | 索引状态,,建索引时不需要使用。取值如下: |
SecondaryIndex
使用构造函数传入索引名称和对应的字段名称即可
Java
1SecondaryIndex(String indexName, String fieldName)
VectorIndex
使用构造函数传入对应参数即可,对应参数也拥有对应的builder构造函数
Java
1public VectorIndex(String indexName, String fieldName, IndexType indexType, IndexState state,MetricType metricType, IndexParams params, Boolean autoBuild, AutoBuildPolicy autoBuildPolicy)
删除表
功能介绍
删除指定的表。
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4public class Main {
5 public static void main(String[] args) {
6 String account = "root";
7 String apiKey = "*********";
8 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
9 ClientConfiguration clientConfiguration = new ClientConfiguration();
10 clientConfiguration.setCredentials(new Credentials(account, apiKey));
11 clientConfiguration.setEndpoint(endpoint);
12 MochowClient mochowClient = new MochowClient(clientConfiguration);
13 String databaseName = "test";
14 String tableName = "test";
15 mochowClient.dropTable(databaseName, tableName);
16 }
17}
请求参数
参数 | 参数类型 | 是否必选 | 参数含义 |
---|---|---|---|
databaseName | String | 是 | 库名 |
tableName | String | 是 | 表名 |
查询指定表详情
功能介绍
查询指定表的详情。
请求示例
Java
1import com.baidu.mochow.auth.Credentials;
2import com.baidu.mochow.client.ClientConfiguration;
3import com.baidu.mochow.client.MochowClient;
4import com.baidu.mochow.model.DescribeTableResponse;
5import com.baidu.mochow.util.JsonUtils;
6public class Main {
7 public static void main(String[] args) {
8 String account = "root";
9 String apiKey = "*********";
10 String endpoint = "*.*.*.*:*"; // example: 127.0.0.1:5287
11 ClientConfiguration clientConfiguration = new ClientConfiguration();
12 clientConfiguration.setCredentials(new Credentials(account, apiKey));
13 clientConfiguration.setEndpoint(endpoint);
14 MochowClient mochowClient = new MochowClient(clientConfiguration);
15 String databaseName = "test";
16 String tableName = "test";
17 DescribeTableResponse response = mochowClient.describeTable(databaseName, tableName);
18 System.out.printf("Describe table response: %s\n", JsonUtils.toJsonString(response));
19 }
20}
相关类说明
DescribeTableResponse
描述表的状态,拥有getter等通用方法,通用方法可参考通用方法详细说明。
Java
1import com.baidu.mochow.model.DescTableRequest; //导入包
成员名 | 类型 | 是否必填 | 成员含义 |
---|---|---|---|
table | Table | 是 | 表的详细信息 |
Table
表的详细信息,拥有constructor、getter、setter等通用方法,通用方法详情可以参考通用说明
Java
1import com.baidu.mochow.model.DescTableRequest; //导入包
参数 | 参数类型 | 参数含义 |
---|---|---|
database | String | 库的名称。 |
table | String | 表的名称。 |
replication | Int | 单个分区的总副本数(含主副本)。 |
partition | PartitionParams | 表的分区数量。 |
schema | Schema | 表的Schema信息。 |
enableDynamicField | Boolean | 表是否支持自动增加字段。 |
description | String | 表的描述信息。 |
createTime | Int | 表的创建时间。 |
state | TableState | 表的当前状态,取值如下: |
aliases | List<String> | 表的别名列表。 |