首页 > 其他分享 >milvus的db和collection信息查询

milvus的db和collection信息查询

时间:2023-12-27 18:02:43浏览次数:25  
标签:github fmt db collection go indirect com milvus

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

标签:github,fmt,db,collection,go,indirect,com,milvus
From: https://blog.51cto.com/huangzhimao/9002636

相关文章

  • SQL SERVER 数据库的常用DBA命令
    查看表是否死锁select* frommaster..SysProcesses wheredb_Name(dbID)='数据库名'  andspId<>@@SpId  anddbID<>0  andblocked>0;或者selectrequest_session_idspid,OBJECT_NAME(resource_associated_entity_id)table......
  • adb命令安装
    下载adb工具下载地址:https://pan.baidu.com/s/1mgGkNZM下载后是个压缩包,解压后一共有5个文件,如下图:这里我都是解压到当前文件夹了  ###查看adb工具是否安装成功直接在安装路径中点开cmd.exe,输入adbversion,出现如下的版本号就算是安装成功了 1.win+R,输入cmd回车2.......
  • 【汇总】android adb 命令功能大全
    前言全局说明adb命令是Android官方提供,调试Android系统的工具。一、基础命令adbreboot#重启adbhelp#查看ADB帮助二、查看设备adbdevices#查看连接设备三、连接设备adb[-d|-e|-s<serialNumber>]<command>连接指定设备参数:-d指定当前唯一通过USB......
  • 解决Linux环境下Android调试adb没有权限问题
    转载自:https://codeleading.com/article/1564378047/问题描述adbdevicesnopermission(userxxxisnotintheplugdevgroup);see[http://developer.android.com/tools/device.html]解决方案在/etc/udev/rules.d/目录下创建文件:51-android.rules,并在文件中填入如......
  • Spring系列:基于Spring-Jdbc实现事务
    目录一、事务基本概念二、编程式事务三、声明式事务前期准备四、基于注解的声明式事务@Transactional注解标识的位置事务属性:只读事务属性:超时事务属性:回滚策略事务属性:隔离级别事务属性:传播行为测试五、基于XML的声明式事务一、事务基本概念①什么是事务数据库事务(transacti......
  • 快速实现Modbus和Profinet互转的方案
    快速实现Modbus和Profinet互转的方案为了快速实现将Modbus信号转换为Profinet信号的畅通无阻,我们可以使用Modbus转Profinet网关(XD-MDPN100/200)。Modbus转Profinet网关(XD-MDPN100/200)可以实现快速的协议转换,将Modbus信号转换为Profinet信号,并且在转换过程中确保通信的稳定和安全。......
  • mongodb安装
    #!/bin/bash###############################################################FileName:install_redis.sh#Version:V1.0#Author:junwang#Organization:#CreatedTime:2021-04-1417:12:54#Description:###############################################......
  • P2865 [USACO06NOV] Roadblocks G
    原题链接题解1.在处理最短路的时候,我们采用优先队列的方法,即第一个出现的点一定是最小的,之后出现的点都是在其他点的基础上叠加的值,肯定不小于第一个。那么依然是这个思路,第二个出现的点一定是次短的。代码#include<bits/stdc++.h>usingnamespacestd;structunit{in......
  • Modbus转Profinet网关解决设备通讯不稳的问题
    Modbus转Profinet网关解决设备通讯不稳的问题通讯不稳定:表现为数据断断续续,多半是由于线路干扰、接口不匹配、程序不稳定、等原因造成。解决方案:在原配电柜添加Modbus转Profinet网关(XD-MDPN100/2000)即可解决通迅该问题,Modbus转Profinet网关(XD-MDPN100/2000)具有抗干扰功能,采用映射......
  • 无涯教程-Java9 - Collection工厂方法
    使用Java9,新的工厂方法被添加到List,Set和Map接口以创建不可变的实例。用于以较少的冗长和简洁的方式创建集合。Collections旧方法importjava.util.ArrayList;importjava.util.Collections;importjava.util.HashMap;importjava.util.HashSet;importjava.util.List;im......