milvus的db和collection信息查询

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

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

目录
相关文章
|
开发工具 Python
milvus的delete操作
milvus的delete操作
1963 0
|
存储 Linux 数据安全/隐私保护
安装部署milvus单机版(快速体验)
安装部署milvus单机版(快速体验)
5876 0
|
10月前
|
弹性计算 安全 Linux
阿里云服务器镜像解析:公共、自定义、共享、云市场及社区镜像对比与选择参考
阿里云服务器ESC镜像包括公共、自定义、共享、云市场及社区五大类型,每种镜像具有不同的特性和适用场景。公共镜像安全稳定;自定义镜像量身定制,可快速部署;共享镜像可跨账号协作;云市场镜像一键部署,省时省心;社区镜像开放共享,满足个性化需求。选择镜像时,用户需考虑操作系统、初始配置、安全性、稳定性及成本。
|
存储 JSON 对象存储
零门槛玩转向量引擎!阿里云 Milvus 无代码全流程实操指南
阿里云Milvus版是企业级向量引擎,支持非结构化数据语义检索。全托管架构、开源兼容,助力智能驾驶、电商推荐、智能客服等场景实现毫秒级精准匹配,无代码操作让AI落地更高效。
1555 0
|
11月前
|
JSON Java 数据格式
String转JSON
该代码段演示了如何将字符串解析为JSON对象。通过`JSONObject.fromObject()`方法将包含中文的JSON字符串转换为JSONObject实例,并输出其标准格式化内容,适用于Java中处理JSON数据的场景。
|
传感器 运维 前端开发
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
本文解析异常(anomaly)与新颖性(novelty)检测的本质差异,结合distfit库演示基于概率密度拟合的单变量无监督异常检测方法,涵盖全局、上下文与集体离群值识别,助力构建高可解释性模型。
800 10
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
|
人工智能 JavaScript 前端开发
用 Go 语言轻松构建 MCP 服务器
本文介绍了使用 Go 语言构建 MCP 服务器的完整过程,涵盖创建服务器实例、注册工具、资源和提示词,以及通过 stdio 和 sse 模式启动服务的方法,帮助开发者快速集成 LLM 应用与外部系统。
|
人工智能 前端开发 JavaScript
Ant Design X ✖️ 百宝箱智能体 SDK 极速构建 AI 聊天应用
本文介绍如何使用 Ant Design X 组件库与百宝箱 SDK 快速搭建 AI 对话应用。前端采用 React + Ant Design X,提供消息气泡、智能输入框等丰富组件;后端通过百宝箱 SDK 接入主流大模型,支持流式响应与高效数据管理,实现极简代码开发、快速上线的智能聊天应用。
1494 0
|
Kubernetes Docker 容器
Job for docker.service failed because the control process exited with error code.
Job for docker.service failed because the control process exited with error code.
3896 0