Milvus的安装和使用
  Adknp2DJyaqB 2023年11月02日 32 0

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())
	}

}

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jE8iBWyhLcsg   2023年11月02日   46   0   0 javasubstringgolang
  Adknp2DJyaqB   2023年11月02日   55   0   0 unsafegolang
  Adknp2DJyaqB   2023年11月02日   34   0   0 unicodegolang
  Ohl6n170bzPf   2023年11月02日   26   0   0 golang
  Adknp2DJyaqB   2023年11月13日   19   0   0 restfulgrpcgolang
  Adknp2DJyaqB   2023年11月13日   30   0   0 发布订阅grpcgolang
  Adknp2DJyaqB   2023年11月02日   31   0   0 golang
  Adknp2DJyaqB   2023年11月02日   33   0   0 golangmilvus
  Adknp2DJyaqB   2023年11月13日   22   0   0 grpcgolang
Adknp2DJyaqB