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

目录
相关文章
|
存储 SQL API
milvus insert api流程源码分析
milvus insert api流程源码分析
624 3
|
存储 Linux 数据安全/隐私保护
安装部署milvus单机版(快速体验)
安装部署milvus单机版(快速体验)
3760 0
|
人工智能 自然语言处理 API
向量检索服务实践测评
向量检索服务是一种基于阿里云自研的向量引擎 Proxima 内核,提供具备水平拓展、全托管、云原生的高效向量检索服务。向量检索服务将强大的向量管理、查询等能力,通过简洁易用的 SDK/API 接口透出,方便在大模型知识库搭建、多模态 AI 搜索等多种应用场景上集成。
139121 5
|
开发工具 Python
milvus的delete操作
milvus的delete操作
1648 0
|
10月前
|
存储 人工智能 算法
通过Milvus内置Sparse-BM25算法进行全文检索并将混合检索应用于RAG系统
阿里云向量检索服务Milvus 2.5版本在全文检索、关键词匹配以及混合检索(Hybrid Search)方面实现了显著的增强,在多模态检索、RAG等多场景中检索结果能够兼顾召回率与精确性。本文将详细介绍如何利用 Milvus 2.5 版本实现这些功能,并阐述其在RAG 应用的 Retrieve 阶段的最佳实践。
1863 1
通过Milvus内置Sparse-BM25算法进行全文检索并将混合检索应用于RAG系统
|
存储 API
milvus insert api的数据结构源码分析
milvus insert api的数据结构源码分析
1393 6
milvus insert api的数据结构源码分析
|
Cloud Native 关系型数据库 新能源
|
存储 人工智能 运维
阿里云向量检索服务 Milvus 版正式商业化
阿里云向量检索服务 Milvus 版正式商业化!
|
存储 Cloud Native NoSQL
向量数据库汇总
向量数据库汇总
1192 0