Table 操作
更新时间:2026-09-01
创建表
功能介绍
在指定的库中新建一个表。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7 "github.com/baidu/mochow-sdk-go/mochow/api"
8)
9
10func main() {
11 clientConfig := &mochow.ClientConfiguration{
12 Account: "root",
13 APIKey: "您的账户API密钥",
14 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
15 }
16
17 // create mochow client
18 client, err := mochow.NewClientWithConfig(clientConfig)
19 if err != nil {
20 log.Fatalf("Fail to init mochow client due to error:%v", err)
21 return
22 }
23
24 // Fields
25 fields := []api.FieldSchema{
26 {
27 FieldName: "id",
28 FieldType: api.FieldTypeString,
29 PrimaryKey: true,
30 PartitionKey: true,
31 AutoIncrement: false,
32 NotNull: true,
33 },
34 {
35 FieldName: "bookName",
36 FieldType: api.FieldTypeString,
37 NotNull: true,
38 },
39 {
40 FieldName: "author",
41 FieldType: api.FieldTypeString,
42 NotNull: true,
43 },
44 {
45 FieldName: "page",
46 FieldType: api.FieldTypeUint32,
47 NotNull: true,
48 },
49 {
50 FieldName: "segment",
51 FieldType: api.FieldTypeText,
52 NotNull: true,
53 },
54 {
55 FieldName: "vector",
56 FieldType: api.FieldTypeFloatVector,
57 NotNull: true,
58 Dimension: 3,
59 },
60 }
61
62 // Indexes
63 autoBuildPolicy := api.NewAutoBuildIncrementPolicy()
64 autoBuildPolicy.AddRowCountIncrement(5000)
65 indexes := []api.IndexSchema{
66 {
67 IndexName: "book_name_idx",
68 Field: "bookName",
69 IndexType: api.SecondaryIndex,
70 },
71 {
72 IndexName: "filtering_idx",
73 IndexType: api.FilteringIndex,
74 FilterIndexFields: []api.FilteringIndexField{
75 {
76 Field: "bookName",
77 IndexStructureType: api.IndexStructureTypeDefault,
78 },
79 {
80 // AGGREGATED_BITMAP 除等值查询外还可加速范围查询
81 Field: "page",
82 IndexStructureType: api.IndexStructureTypeAggregatedBitmap,
83 },
84 },
85 },
86 {
87 // 持久化BITMAP索引,索引数据基于磁盘存储,用于等值过滤加速
88 IndexName: "page_persistent_bitmap_idx",
89 IndexType: api.PersistentBitmapIndex,
90 Field: "page",
91 },
92 {
93 // 持久化聚合BITMAP索引,同时支持等值与范围过滤加速
94 // 必须指定 FanoutBits 与 MaxDepth
95 IndexName: "page_persistent_aggregated_bitmap_idx",
96 IndexType: api.PersistentAggregatedBitmapIndex,
97 Field: "page",
98 FanoutBits: 2,
99 MaxDepth: 10,
100 },
101 {
102 // 倒排索引(全文索引),支持自定义停用词表
103 IndexName: "segment_inverted_idx",
104 IndexType: api.InvertedIndex,
105 InvertedIndexFields: []string{"segment"},
106 InvertedIndexFieldAttributes: []api.InvertedIndexFieldAttribute{api.Analyzed},
107 Params: api.NewInvertedIndexParams().
108 Analyzer(api.DefaultAnalyzer).
109 ParseMode(api.CoarseMode).
110 AnalyzerCaseSensitive(true).
111 StopWords(
112 api.StopWordsParams{}.New(api.CustomStopWords).
113 Words([]string{"呀", "啊", "哦"}),
114 ),
115 },
116 {
117 IndexName: "vector_idx",
118 Field: "vector",
119 IndexType: api.HNSW,
120 MetricType: api.L2,
121 Params: api.VectorIndexParams{
122 "M": 16,
123 "efConstruction": 200,
124 },
125 AutoBuild: true,
126 AutoBuildPolicy: autoBuildPolicy.Params(),
127 },
128 }
129
130 // create table
131 createTableArgs := &api.CreateTableArgs{
132 Database: "db_test",
133 Table: "table_test",
134 Replication: 3,
135 Partition: &api.PartitionParams{
136 PartitionType: api.HASH,
137 PartitionNum: 3,
138 },
139 EnableDynamicField: false,
140 Schema: &api.TableSchema{
141 Fields: fields,
142 Indexes: indexes,
143 },
144 }
145 if err := client.CreateTable(createTableArgs); err != nil {
146 log.Fatalf("Fail to create table due to error: %v", err)
147 return
148 }
149}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| createTableArgs | CreateTableArgs | 是 | 创建表参数。 |
CreateTableArgs
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Database | String | 是 | 指定库的名称。库名称命名要求如下: 支持大小写字母、数字以及_特殊字符,必须以字母开头; 长度限制为1~255。 表的字段详情列表。 |
| Table | String | 是 | 指定表的名称。表的命名要求如下: 仅支持大小写字母、数字以及下划线(_),且必须以字母开头; 长度限制为1~255。 表的索引详情列表。 |
| Description | String | 否 | 数据表的描述。 |
| Replication | Int | 是 | 单个分区的总副本数(含主副本),取值范围为[1,10]。 若需要高可用特性,副本总数需>=3。 注意:副本数需要小于等于数据节点的数量,否则无法建表。 |
| Partition | PartitionParams | 是 | 表的分区数量,取值范围为[1, 1000]。 |
| 若非FLAT索引,则建议将单个分区的记录总数控制在100万到1000万之间,过大过小都不太合适。 | |||
| EnableDynamicField | Boolean | 否 | 表是否支持自动增加字段,默认值为False。 |
| Schema | TableSchema | 是 | 表的Schema信息。 |
PartitionParams
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| PartitionType | String | 分区类型枚举,当前仅支持"HASH"。 |
| PartitionNum | Int | 分区的数量,取值范围为[1,1000]。 |
TableSchema
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| Fields | List |
数据表中字段的定义。 |
| Indexes | List |
数据表中索引的定义。 |
FieldSchema
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| FieldName | String | 字段名称,要求表内唯一。 字段命名要求如下: 仅支持大小写字母、数字以及下划线(_),必须以字母开头; 长度限制为1~255。 |
| FieldType | String | 字段类型,当前支持如下类型: BOOL INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 FLOAT DOUBLE DATE DATETIME TIMESTAMP UUID STRING BINARY FLOAT_VECTOR 各数据类型的详细定义和约束请参见“产品介绍”目录的“数据类型”页面。 |
| PrimaryKey | Boolean | 是否为主键,默认值为False。 当前仅支持单一字段作为主键。 主键字段不支持如下类型:BOOL、FLOAT、DOUBLE和FLOAT_VECTOR。 |
| PartitionKey | Boolean | 是否为分区键,默认值为False。 当前仅支持单一字段作为分区键,分区键可以是主键,也可以不是主键,但一张表只能有一个分区键,每行记录都会根据分区键的取值哈希映射到不同的分区。 分区键字段不支持如下类型:BOOL、FLOAT、DOUBLE和FLOAT_VECTOR。 |
| AutoIncrement | Boolean | 是否自增主键,默认值为False。 仅适用于类型为UINT64的主键字段,非主键字段请勿填写属性值。 |
| NotNull | Boolean | 是否非空,默认值为False。 不可以为空值的字段包括:主键字段、分区键字段、向量字段和索引键字段。 |
| Dimension | Int | 向量维度,仅当字段类型为FLOAT_VECTOR时,才需要指定该参数。 |
| ElementType | String | 数组类型字段的元素类型。 仅当字段类型为ARRAY时,才需要指定该参数。 |
| MaxCapacity | Int | 数组类型字段的最大容量,默认不限制。 仅当字段类型为ARRAY时,才需要指定该参数。 |
IndexSchema
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| IndexName | String | 索引名称,要求表内唯一。 索引命名要求如下: 仅支持大小写字母、数字以及下划线(_),必须以字母开头; 长度限制为1~255。 |
| IndexType | String | 索引类型。当前支持的类型如下: 非向量索引类型: 向量索引类型: 对应的Go常量分别为api.SecondaryIndex、api.FilteringIndex、api.PersistentBitmapIndex、api.PersistentAggregatedBitmapIndex、api.InvertedIndex、api.FLAT、api.HNSW、api.HNSWPQ、api.HNSWSQ、api.HNSWRABITQ、api.PUCK、api.IVF、api.IVFSQ、api.IVFPQ、api.IVFRABITQ、api.DISKANN、api.SPARSE。 |
| MetricType | String | 向量索引的距离度量算法。支持的类型如下: L2:欧几里得距离 IP:内积距离 COSINE:余弦距离 |
| Params | VectorIndexParams | 向量构建索引所需参数。 1. M:表示每个节点在检索构图中可以连接多少个邻居节点。取值为[4, 128]; 2. efConstruction:搜索时,指定寻找节点邻居遍历的范围。数值越大构图效果越好,构图时间越长。取值为[8, 1024]。 1. coarseClusterCount:索引中粗聚类中心的个数; 2. fineClusterCount:每个粗聚类中心下细聚类中心个数。 1. M:取值为[4, 128]; 2. efConstruction:取值为[8, 1024]; 3. NSQ:表示量化子空间个数,取值为[1, dim],并且要求NSQ | dim; 4. sampleRate:kmeans训练原始数据的抽样比率,取值为[0.0, 1.0],抽样总数 10000 + (rowCount - 10000)*sampleRate。 1. M; 2. efConstruction; 3. qtBits:每个维度SQ量化所用的bit数,取值为{8, 16}。 1. M:取值为[4, 128]; 2. efConstruction:取值为[8, 1024]。 1. NSQ:取值范围为1 <= NSQ <= 512 且 NSQ <= dim; 2. R:vamana图的出度,取值范围为[1, 128]; 3. L:vamana图构建期间候选集大小,取值范围为[8, 1024]。 nlist:聚类数量,取值范围为1 <= nlist <= 65536。 1. nlist:聚类数量,取值范围为1 <= nlist <= 65536; 2. qtBits:每个维度SQ量化所用的bit数,取值为{4, 8, 16}。 1. nlist:聚类数量,取值范围为1 <= nlist <= 65536; 2. NSQ:表示量化子空间个数,要求NSQ整除有效向量维度。 nlist:聚类数量,取值范围为1 <= nlist <= 65536。 倒排索引的构建参数通过api.NewInvertedIndexParams()构造,详见下文InvertedIndexParams。 |
| AutoBuild | Bool | 是否自动构建索引 |
| AutoBuildPolicy | AutoBuildPolicy | 自动构建索引策略,当前支持如下策略: AutoBuildTiming:定时构建,指定构建的时间,构建一次,不会重复构建。例如 AutoBuildPeriodical:周期性构建,每过period_s秒构建一次索引,可重复构建。可以指定从某个时间点开始。 AutoBuildRowCountIncrement:增量行数构建。Tablet(不是table)增加或者减少指定的行数时会自动构建一次索引,可重复构建,支持具体行数以及百分比,只需传入一种即可,也可传入两种,触发其中之一便会开始构建。 |
| Field | String | 索引作用于的目标字段名称。当索引类型为向量索引、SECONDARY、PERSISTENT_BITMAP或PERSISTENT_AGGREGATED_BITMAP时需要填写。 |
| FanoutBits | Uint32 | PERSISTENT_AGGREGATED_BITMAP索引的必填参数,表示聚合BITMAP树每层用于分叉的位数,取值范围为[1, 10],且需要满足 FanoutBits * (MaxDepth - 1) <= 64。 |
| MaxDepth | Uint32 | PERSISTENT_AGGREGATED_BITMAP索引的必填参数,表示聚合BITMAP树的最大深度,取值范围为[2, 30],且需要满足 FanoutBits * (MaxDepth - 1) <= 64。 |
| InvertedIndexFields | List |
倒排索引作用于的目标字段名称。 |
| InvertedIndexFieldAttributes | List |
指定建立倒排索引的列是否需要分词(默认是会分词),参数顺序应与'fields'里列名一一对应。目前支持以下选项: ATTRIBUTE_ANALYZED ATTRIBUTE_NOT_ANALYZED |
| FilterIndexFields | List |
Filtering索引作用于的目标字段名称。 |
FilteringIndexField
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Field | String | 是 | 索引作用于的目标字段名称。 支持以下通配符: |
| IndexStructureType | IndexStructureType | 否 | 选择FILTERING索引的内存结构。支持的类型如下: IndexStructureType的缺省值为DEFAULT。如果指定了通配符@SCALAR,则使用@SCALAR字段中的IndexStructureType作为缺省值。 |
InvertedIndexParams
通过api.NewInvertedIndexParams()创建,并以链式调用方式设置各参数。
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| Analyzer | InvertedIndexAnalyzer | 否 | 指定倒排索引的分词器。目前支持以下三种: |
| ParseMode | InvertedIndexParseMode | 否 | 分词器的分词模式。 |
| AnalyzerCaseSensitive | Bool | 否 | 是否大小写敏感。 |
| StopWords | StopWordsParams | 否 | 停用词表配置。停用词是指“呀”“啊”“哦”这类被认为无检索意义、不会加入倒排索引的词。未配置时等价于使用系统默认停用词表(DEFAULT)。 注:停用词只能在建表或创建索引时指定,创建后不支持原地修改,检索请求也不能临时覆盖。 |
StopWordsParams
通过api.StopWordsParams{}.New(mode)创建,可继续调用Words()设置自定义词表。
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| mode | StopWordsMode | 是 | 停用词模式。取值如下:Words()指定非空词表。 |
| Words | List |
否 | 自定义停用词列表,仅在mode为CUSTOM时生效且不能为空。每个词条应为单个term。 |
注意事项
- 建表时同时创建倒排索引(全文索引)时,索引会异步构建:构建完成前通过查询索引详情看到的
State为BUILDING,此时发起BM25/Hybrid检索会返回错误码95(Index Building);State变为NORMAL后方可用于检索。 - PERSISTENT_BITMAP与PERSISTENT_AGGREGATED_BITMAP是独立的
IndexType,不是FILTERING索引的IndexStructureType,需要通过Field指定单个目标字段。
删除表
功能介绍
删除指定的数据表。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7)
8
9func main() {
10 clientConfig := &mochow.ClientConfiguration{
11 Account: "root",
12 APIKey: "您的账户API密钥",
13 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
14 }
15
16 // create mochow client
17 client, err := mochow.NewClientWithConfig(clientConfig)
18 if err != nil {
19 log.Fatalf("Fail to init mochow client due to error:%v", err)
20 return
21 }
22
23 if err := client.DropTable("db_test", "table_test"); err != nil {
24 log.Fatalf("Fail to drop table due to error: %v", err)
25 return
26 }
27}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| databaseName | String | 是 | 目标库的名称 |
| tableName | String | 是 | 目标表的名称。 |
查询表详情
功能介绍
查询指定数据表的详情。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7)
8
9func main() {
10 clientConfig := &mochow.ClientConfiguration{
11 Account: "root",
12 APIKey: "您的账户API密钥",
13 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
14 }
15
16 // create mochow client
17 client, err := mochow.NewClientWithConfig(clientConfig)
18 if err != nil {
19 log.Fatalf("Fail to init mochow client due to error:%v", err)
20 return
21 }
22
23 describeTableResult, err := client.DescTable("db_test", "table_test")
24 if err != nil {
25 log.Fatalf("Fail to describe table due to error: %v", err)
26 return
27 }
28 log.Printf("describe table response: %v", describeTableResult)
29}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| dabaseName | String | 是 | 目标库的名称 |
| tableName | String | 是 | 目标表的名称。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| describeTableResult | DescTableResult | Table详情 |
DescTableResult
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| Database | String | 指定库的名称。库名称命名要求如下: 支持大小写字母、数字以及_特殊字符,必须以字母开头; 长度限制为1~255。 表的字段详情列表。 |
| Table | String | 指定表的名称。表的命名要求如下: 仅支持大小写字母、数字以及下划线(_),且必须以字母开头; 长度限制为1~255。 表的索引详情列表。 |
| Description | String | 数据表的描述。 |
| Replication | Int | 单个分区的总副本数(含主副本),取值范围为[1,10]。 若需要高可用特性,副本总数需>=3。 注意:副本数需要小于等于数据节点的数量,否则无法建表。 |
| Partition | PartitionParams | 表的分区数量,取值范围为[1, 1000]。 若非FLAT索引,则建议将单个分区的记录总数控制在100万到1000万之间,过大过小都不太合适。 |
| EnableDynamicField | Boolean | 表是否支持自动增加字段,默认值为False。 |
| Schema | TableSchema | 表的Schema信息。 |
| State | TableState | 表的当前状态,取值如下: CREATING:表处于创建中 NORMAL:表状态正常 DELETING:表正在被删除 |
查询表的列表
功能介绍
查询指定库包含的所有表。
请求示例
Go
1package main
2
3import (
4 "log"
5
6 "github.com/baidu/mochow-sdk-go/mochow"
7)
8
9func main() {
10 clientConfig := &mochow.ClientConfiguration{
11 Account: "root",
12 APIKey: "您的账户API密钥",
13 Endpoint: "您的实例访问端点", // 例如:'http://127.0.0.1:5287'
14 }
15
16 // create mochow client
17 client, err := mochow.NewClientWithConfig(clientConfig)
18 if err != nil {
19 log.Fatalf("Fail to init mochow client due to error:%v", err)
20 return
21 }
22
23 listTableResult, err := client.ListTable("db_test")
24 if err != nil {
25 log.Fatalf("Fail to list table due to error: %v", err)
26 return
27 }
28 log.Printf("list table response: %v", listTableResult)
29}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数含义 |
|---|---|---|---|
| databaseName | String | 是 | 库的名称。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| listTableResult | ListTableResult | 表对象列表。 |
ListTableResult
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| Tables | List |
表名列表 |
评价此篇文章
