milvus init
This commit is contained in:
		
							
								
								
									
										45
									
								
								models/milvus.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								models/milvus.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"log"
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	"github.com/milvus-io/milvus-sdk-go/v2/client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type MilvusConnection struct {
 | 
			
		||||
	Client client.Client
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *MilvusConnection) GetClient() client.Client {
 | 
			
		||||
	return m.Client
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *MilvusConnection) Init() (err error) {
 | 
			
		||||
	log.Println("Milvus connection init")
 | 
			
		||||
	host := Viper.Get("milvus.host").(string)
 | 
			
		||||
	port := Viper.Get("milvus.port").(int)
 | 
			
		||||
	address_of_milvus := host + ":" + strconv.Itoa(port)
 | 
			
		||||
	log.Println("Milvus address:", address_of_milvus)
 | 
			
		||||
	m.Client, err = client.NewGrpcClient(context.Background(), address_of_milvus)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println("Milvus connection failed:", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	log.Println("Milvus connection success")
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *MilvusConnection) GetCollection(collection_name string) (collection *client.Client, err error) {
 | 
			
		||||
	if m.Client == nil {
 | 
			
		||||
		m.Init()
 | 
			
		||||
	}
 | 
			
		||||
	err = m.Client.LoadCollection(context.Background(), collection_name, false)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println("Milvus load collection failed:", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	collection = &m.Client
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
@@ -9,23 +9,17 @@ import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 初始化数据库连接
 | 
			
		||||
//func (m *MysqlConnectionx) Init() {
 | 
			
		||||
//	fmt.Println("初始化数据库连接")
 | 
			
		||||
//}
 | 
			
		||||
 | 
			
		||||
type MysqlConnection struct {
 | 
			
		||||
	Database *sql.DB
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 初始化数据库连接
 | 
			
		||||
func (m *MysqlConnection) Init() (err error) {
 | 
			
		||||
	viper := Viper // 从 models/config.go 中获取 viper 对象
 | 
			
		||||
	user := viper.Get("mysql.user").(string)
 | 
			
		||||
	password := viper.Get("mysql.password").(string)
 | 
			
		||||
	host := viper.Get("mysql.host").(string)
 | 
			
		||||
	port := viper.Get("mysql.port").(int)
 | 
			
		||||
	database := viper.Get("mysql.database").(string)
 | 
			
		||||
	user := Viper.Get("mysql.user").(string)
 | 
			
		||||
	password := Viper.Get("mysql.password").(string)
 | 
			
		||||
	host := Viper.Get("mysql.host").(string)
 | 
			
		||||
	port := Viper.Get("mysql.port").(int)
 | 
			
		||||
	database := Viper.Get("mysql.database").(string)
 | 
			
		||||
	sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local"
 | 
			
		||||
	m.Database, err = sql.Open("mysql", sqlconf) // 连接数据库
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user