Milvus的安装和是用
主要是使用docker compose
1. 下载编排文件
mkdir milvus
cd milvus
wget https://github.com/milvus-io/milvus/releases/download/v2.3.1/milvus-standalone-docker-compose.yml -O docker-compose.yml
2. 启动容器
docker compose up
docker compose up -d //后台启动
3. 查看启动容器
df39d8b0cfc9 milvusdb/milvus:v2.3.1 "/tini -- milvus run…" 13 minutes ago Up 13 minutes (healthy) 0.0.0.0:9091->9091/tcp, :::9091->9091/tcp, 0.0.0.0:19530->19530/tcp, :::19530->19530/tcp milvus-standalone
8584c5da2f8c minio/minio:RELEASE.2023-03-20T20-16-18Z "/usr/bin/docker-ent…" 13 minutes ago Up 13 minutes (healthy) 0.0.0.0:9000-9001->9000-9001/tcp, :::9000-9001->9000-9001/tcp milvus-minio
2ebb7074ea61 quay.io/coreos/etcd:v3.5.5 "etcd -advertise-cli…" 13 minutes ago Up 13 minutes (healthy) 2379-2380/tcp milvus-etcd
docker port milvus-standalone 19530/tcp
4. golang
package main
import (
"context"
"fmt"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
"log"
"math/rand"
)
func main() {
//grpc 连接
milvusClient, err := client.NewGrpcClient(
context.Background(), // ctx
"192.168.4.81:19530", // addr
)
defer milvusClient.Close()
if err != nil {
log.Fatal("failed to connect to Milvus:", err.Error())
}
var (
collectionName = "book"
)
schema := &entity.Schema{
CollectionName: collectionName,
Description: "Test book search",
Fields: []*entity.Field{
{
Name: "book_id",
DataType: entity.FieldTypeInt64,
PrimaryKey: true,
AutoID: false,
},
{
Name: "word_count",
DataType: entity.FieldTypeInt64,
PrimaryKey: false,
AutoID: false,
},
{
Name: "book_intro",
DataType: entity.FieldTypeFloatVector,
TypeParams: map[string]string{
"dim": "2",
},
},
},
EnableDynamicField: true,
}
//创建集合
err = milvusClient.CreateCollection(
context.Background(), // ctx
schema,
2, // shardNum
)
if err != nil {
log.Fatal("failed to create collection:", err.Error())
}
bookIDs := make([]int64, 0, 2000)
wordCounts := make([]int64, 0, 2000)
bookIntros := make([][]float32, 0, 2000)
for i := 0; i < 2000; i++ {
bookIDs = append(bookIDs, int64(i))
wordCounts = append(wordCounts, int64(i+10000))
v := make([]float32, 0, 2)
for j := 0; j < 2; j++ {
v = append(v, rand.Float32())
}
bookIntros = append(bookIntros, v)
}
idColumn := entity.NewColumnInt64("book_id", bookIDs)
wordColumn := entity.NewColumnInt64("word_count", wordCounts)
introColumn := entity.NewColumnFloatVector("book_intro", 2, bookIntros)
_, err = milvusClient.Insert(
context.Background(), // ctx
"book", // CollectionName
"", // partitionName
idColumn, // columnarData
wordColumn, // columnarData
introColumn, // columnarData
)
if err != nil {
log.Fatal("failed to insert data:", err.Error())
}
err = milvusClient.LoadCollection(
context.Background(), // ctx
"book", // CollectionName
false, // async
)
if err != nil {
log.Fatal("failed to load collection:", err.Error())
}
opt := client.SearchQueryOptionFunc(func(option *client.SearchQueryOption) {
option.Limit = 3
option.Offset = 0
option.ConsistencyLevel = entity.ClStrong
option.IgnoreGrowing = false
})
queryResult, err := milvusClient.Query(
context.Background(), // ctx
"book", // CollectionName
[]string{}, // PartitionName
"", // expr
[]string{"book_id", "book_intro"}, // OutputFields
opt, // queryOptions
)
if err != nil {
log.Fatal("fail to query collection:", err.Error())
}
fmt.Printf("%#v\n", queryResult)
for _, qr := range queryResult {
fmt.Println(qr.Name())
}
}