以下是一个简单的golang milvus数据插入案例,其中假设您已经安装了milvus和golang SDK,并且已经创建了milvus实例。
//导入所需的包 import (
"fmt"
"github.com/milvus-io/milvus-sdk-go/v2/milvus"
)
func main() {
//连接到milvus实例
client, err := milvus.NewClient(milvus.Address("localhost:19530"))
if err != nil {
fmt.Println(err)
return
}
defer client.Close()
//创建collection
collectionName := "example_collection"
dimension := int64(128)
indexFileSize := int64(1024)
err = client.CreateCollection(
milvus.CollectionName(collectionName),
milvus.Dim(dimension),
milvus.IndexFileSize(indexFileSize),
)
if err != nil {
fmt.Println(err)
return
}
//插入数据
vector1 := []float32{0.1, 0.2, 0.3, ..., 0.9}
vector2 := []float32{1.0, 1.1, 1.2, ..., 1.8}
vectors := [][]float32{vector1, vector2}
ids := []int64{1, 2}
err = client.Insert(
milvus.CollectionName(collectionName),
vectors,
milvus.IDs(ids),
)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("数据插入成功!")
}
在这个案例中,我们首先连接到milvus实例,然后使用CreateCollection方法创建一个名为example_collection的collection。接下来,我们使用Insert方法插入两个具有128维度向量的数据,其中ID为1和2。
注意:这仅是一个简单的示例,实际情况需要根据您的业务需求进行更改和优化。