Milvus 安装、设置权限和使用

中文文档
使用了docker compose 安装 standalone 版本,即单机docker。
pymilvus 一直在升级,一些函数有变化,中文文档有落后,建议看英文文档。

安装

#下载docker-compose.yml 文件
wget https://github.com/milvus-io/milvus/releases/download/v2.4.9/milvus-standalone-docker-compose.yml -O docker-compose.yml

# minio端口如果不用可以注释掉
   #ports:
   #  - "9001:9001"
   #  - "9000:9000"

#standalone 也就是milvus的映射端口,主机端口:容器内端口
#可以修改主机端口,防止冲突,容器内端口不可修改。
    ports:
      - "29530:19530"
      - "29091:9091"

# 启动 Milvus
sudo docker compose up -d

#关闭Milvus
sudo docker compose down

如果 docker compose 不能work,查看一下docker 版本,使用 docker-compose 也许可以。

设置权限 通过用户名和密码进行身份验证

在 配置 Milvus 时,将 milvus.yaml 中的 common.security.authorizationEnabled 设为 true 以启用身份验证

由于使用了docker,milvus.yaml文件在docker容器里。

#进入容器
sudo docker exec -it  Milvus容器ID  /bin/bash
#修改 milvus.yaml
sed -i 's/authorizationEnabled: false/authorizationEnabled: true/g'  configs/milvus.yaml
#退出容器,而不关闭容器
依次按 ctrl+p 和 ctrl+q 

默认用户名:root
默认密码: Milvus

from pymilvus import MilvusClient
#连接
client = MilvusClient(
    uri='http://localhost:29530', # replace with your own Milvus server address
    token="root:Milvus"
) 
#修改密码
client.update_password(
    user_name="root",
    old_password="Milvus",
    new_password="P@ssw0rd123"
)
#创建新用户
client.create_user(
    user_name="user_1",
    password="P@ssw0rd",
)
client.describe_user("user_1")

基本使用

from pymilvus import MilvusClient,DataType

client = MilvusClient(uri="http://localhost:29530",token="用户名:密码")
coll_name = "test_zhishi"

collecitons = client.list_collections()
print(collections)

创建collection

在我使用的这个版本的Milvus docker中,id是必须是有的,即使你不定义;而且 auto_id=False这个参数,设置为True也无效,所以插入数据的时候必须插入id,不然就会报错。 可以通过describe_collection查看schema。

if coll_name  not in collections:
    schema = MilvusClient.create_schema(
        auto_id=False,
        enable_dynamic_field=True,
    )

    schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)
    schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=1024)
    schema.add_field(field_name="text", datatype=DataType.VARCHAR)
    schema.add_field(field_name="subject", datatype=DataType.VARCHAR)

    index_params = client.prepare_index_params(
        field_name="vector",
        index_type="AUTOINDEX",
        metric_type="IP"
    )

    client.create_collection(
        collection_name="test_zhishi",
        schema=schema,
        dimension=1024,
        index_params=index_params
    )

删除collection

status = client.drop_collection(collection_name=coll_name)
print(status)

查看collection

schema = client.describe_collection(collection_name=coll_name)
print(schema)

插入数据

data =[]
for i,doc in enumerate(docs[1:]):
    vector = predict_embedding(doc)
    data.append({'id':i,"vector":vector[0],"text":doc,"subject":"zhishi"})

res = client.insert(collection_name=coll_name,data=data)
print(res)

查询数据

query = ["明天天气怎么样"]
query_embedding = predict_embedding(query)
res = client.search(collection_name=coll_name,data=query_embedding,limit=1,output_fields=['text','subject'])
print(res)

更新数据

res = client.upsert( collection_name=coll_name,  data=data)
print(res)

删除数据

res = client.delete( collection_name=coll_name, filter="id in [4,5,6]")
print(res)
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值