milvus的db和collection信息查询

简介: milvus的db和collection信息查询

2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>


阿里云采购季(云主机223元/3年)活动入口:请点击进入>>>,


阿里云学生服务器(9.5元/月)购买入口:请点击进入>>>,

db和collection信息查询

本文milvus版本:v2.3.2

本项目地址:

https://github.com/melodyfx/milvuslist

attu是一个非常好的管理milvus的图形化web工具。有时候不方便使用的时候可以使用API进行操作。

下图是attu展示的db和collection信息:

hello_milvus.jpg

在这里显示了数据库名称、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()。

运行截图:

milvuslist.jpg

目录
相关文章
|
5月前
|
开发工具 Python
milvus的delete操作
milvus的delete操作
977 0
|
1月前
|
Oracle 关系型数据库 MySQL
【MySQL】8. 基本查询(update/delete/聚合/分组)
【MySQL】8. 基本查询(update/delete/聚合/分组)
38 0
|
6月前
|
NoSQL MongoDB
08 MongoDB - 数据查询(Limit 与 Skip)
08 MongoDB - 数据查询(Limit 与 Skip)
41 0
|
NoSQL 算法 Oracle
Mongo多个Collection的关联操作实现
题记 在mysql,oracle等关系型数据库中,可以通过表之间的关联操作(join, left join, and)实现字段之间的关联操作。 而在mongo非关系型数据库中没有对应的接口。 为此,我们自身实现了1个Mongo db库中的多个collection之间的类笛卡尔操作。(这种需求,多少有些不合理)
329 0
Mongo多个Collection的关联操作实现
|
SQL 关系型数据库 数据库
PostgreSQL sharding : citus 系列4 - DDL 操作规范 (新增DB,TABLE,SCHEMA,UDF,OP,用户等)
标签 PostgreSQL , citus , 新增对象 , 新增数据库 , 新增用户 背景 citus是PG的一个插件,插件主要针对普通SQL(非UTILITY)加HOOK进行了一些ROUTE处理,同时使用UDF对表进行新建分区的操作。
1727 0

相关课程

更多
http://www.vxiaotou.com