db和collection信息查询
本文milvus版本:v2.3.2
本项目地址:
https://github.com/melodyfx/milvuslist
attu是一个非常好的管理milvus的图形化web工具。有时候不方便使用的时候可以使用API进行操作。
下图是attu展示的db和collection信息:
在这里显示了数据库名称、collection名称、load状态、一致性隔离级别、近似数量、描述等信息。
然后我们通过go sdk对其中的一些信息进行展示。
数据库名称、db所属的collection名称、一致性隔离级别、近似数量、精确数量。
load状态展示相对复杂,这里先略过。
go.mod文件内容:
module milvuslist
go 1.20
require (
github.com/go-ini/ini v1.67.0
github.com/milvus-io/milvus-sdk-go/v2 v2.3.2
)
require (
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/getsentry/sentry-go v0.12.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20220503193339-ba3ae3f07e29 // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
config.go文件内容:
[milvus_server]
milvusAddr = 192.168.230.71:19530
username =
password =
main.go文件内容:
package main
import (
"context"
"fmt"
"github.com/go-ini/ini"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
"os"
)
const (
msgFmt = "==== %s ====\n"
)
func printKV(cfg *ini.File) {
secs := cfg.Sections()
for _, s := range secs {
// 排除名为DEFAULT的section
if s.Name() == "DEFAULT" {
continue
}
fmt.Println("打印配置文件:")
fmt.Printf("===== %s =====\n", s.Name())
keys := s.KeyStrings()
for _, key := range keys {
fmt.Printf("%s:%s\n", key, s.Key(key).String())
}
fmt.Println()
}
}
func main() {
ctx := context.Background()
// 1. 加载INI配置文件
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("无法加载配置文件: %v", err)
return
}
printKV(cfg)
// 2. 读取配置项
// 指定section名称
section := cfg.Section("milvus_server")
if section == nil {
fmt.Println("找不到指定的section")
return
}
milvusAddr := section.Key("milvusAddr").String()
username := section.Key("username").String()
password := section.Key("password").String()
fmt.Printf(msgFmt, "start connecting to Milvus")
c, err := client.NewClient(ctx, client.Config{
Address: milvusAddr,
Username: username,
Password: password,
})
if err != nil {
fmt.Printf("failed to connect to milvus, err: %s\n", err.Error())
os.Exit(1)
}
defer c.Close()
dbs, _ := c.ListDatabases(ctx)
for _, db := range dbs {
fmt.Printf(msgFmt, db)
c.UsingDatabase(ctx, db.Name)
colls, _ := c.ListCollections(ctx)
var cns = make([]string, len(colls))
// 设置隔离级别
func1 := func(option *client.SearchQueryOption) {
option.ConsistencyLevel = entity.ClEventually
}
for i := 0; i < len(colls); i++ {
collName := colls[i].Name
// 获取collection隔离级别
ct, _ := c.DescribeCollection(ctx, collName)
// 获取collection近似数量
nums, _ := c.GetCollectionStatistics(ctx, collName)
// 获取collection精确数量
fieldstr := "count(*)"
outFields := []string{
fieldstr}
rs, err := c.Query(ctx, collName, nil, "", outFields, func1)
if err != nil {
fmt.Printf("%s:%s\n", collName, err.Error())
cns[i] = fmt.Sprintf("%s,ConsistencyLevel:%s,approxCount:%s,exactCount:???", collName, ct.ConsistencyLevel.CommonConsistencyLevel().String(), nums["row_count"])
continue
}
column := rs.GetColumn(fieldstr)
count, _ := column.GetAsInt64(0)
cns[i] = fmt.Sprintf("%s,ConsistencyLevel:%s,approxCount:%s,exactCount:%d", collName, ct.ConsistencyLevel.CommonConsistencyLevel().String(), nums["row_count"], count)
}
for i := 0; i < len(cns); i++ {
fmt.Printf("%d: %s\n", (i + 1), cns[i])
}
fmt.Println()
}
}
从代码可以看出获取collection的一些信息主要用到了DescribeCollection()、GetCollectionStatistics(),获取精确数量用到了Query()。
运行截图: