以下是使用Golang在Milvus中插入一行字符串数据的示例代码:
package main
import (
"fmt"
"github.com/milvus-io/milvus-sdk-go/client"
)
func main() {
// 连接Milvus服务器
milvusClient, err := client.NewMilvusClient("localhost", 19530)
if err != nil {
panic(err)
}
defer milvusClient.Close()
// 创建collection
collectionName := "my_collection"
dim := 128
indexParams := "{\"nlist\": 16384}"
_, err = milvusClient.CreateCollection(collectionName, dim, indexParams)
if err != nil {
panic(err)
}
// 插入一行字符串数据
strData := []string{"hello", "world"}
vectorData := make([]float32, dim)
for i := 0; i < len(strData); i++ {
for j := 0; j < dim; j++ {
vectorData[j] = float32(strData[i][j])
}
ids, err := milvusClient.Insert(collectionName, []float32{}, vectorData)
if err != nil {
panic(err)
}
fmt.Printf("Inserted id: %d\n", ids[0])
}
}
在这个示例中,我们首先连接到了Milvus服务器。然后,我们创建了一个新的collection,并指定了向量的维度和索引参数。最后,我们插入了一行字符串数据。具体来说,我们将字符串转换为一个长度为128的向量,其中每个元素都是字符的ASCII码值。我们将这个向量传递给Milvus的Insert方法,并在成功时打印插入的id。