ZBLOG

golang milvus 插入一行字符串数据

以下是使用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。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=421

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?