milvus的collection操作

milvus的collection操作

创建collection

python 复制代码
import uuid

from pymilvus import (
    connections,
    FieldSchema, CollectionSchema, DataType,
    Collection,
)

collection_name = "hello_milvus"
host = "192.168.230.71"
port = 19530
username = ""
password = ""
num_entities, dim = 5000, 3

print("start connecting to Milvus")
connections.connect("default", host=host, port=port,user=username,password=password)

fields = [
    FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=False),
    FieldSchema(name="random", dtype=DataType.DOUBLE),
    FieldSchema(name="comment", dtype=DataType.VARCHAR, max_length=200),
    FieldSchema(name="embeddings", dtype=DataType.FLOAT16_VECTOR, dim=dim)
]

schema = CollectionSchema(fields, "hello_milvus is the simplest demo to introduce the APIs")

print("Create collection `hello_world`")
coll = Collection(collection_name, schema, consistency_level="Bounded",shards_num=1)

print("done")

Collection()是一个构造函数,定义如下:

python 复制代码
class Collection:
    def __init__(
        self,
        name: str,
        schema: Optional[CollectionSchema] = None,
        using: str = "default",
        **kwargs,
    ) -> None:

name -- the name of collection

schema -- the schema of collection, defaults to None.

using -- Milvus connection alias name, defaults to 'default'.

**kwargs - 告诉python接受任意数量的关键字参数到这个字典中。

那milvus这里有哪些key?

看注释。

删除collection

python 复制代码
coll.drop()

函数定义:

python 复制代码
def drop(self, timeout: Optional[float] = None, **kwargs):

有一个timeout参数。

加载collection

功能:加载collection到内存。

python 复制代码
coll.load()

函数定义:

python 复制代码
def load(
        self,
        partition_names: Optional[list] = None,
        replica_number: int = 1,
        timeout: Optional[float] = None,
        **kwargs,
    )

参数说明:

python 复制代码
coll.load() #阻塞
coll.load(_async=True) #非阻塞
utility.wait_for_loading_complete("hello_iterator")

释放collection

从内存中卸载。

python 复制代码
coll.release()

描述collection

获取collection的信息

python 复制代码
infos = coll.describe()
for key, value in infos.items():
    print(key, value)
相关推荐
孟健8 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞10 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽12 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程17 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪17 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook17 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python