1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- from pymilvus import Collection, CollectionSchema, FieldSchema, DataType, connections
- # 连接到 Milvus
- connections.connect("default", host="127.0.0.1", port="19530")
- # 定义新的字段和 schema
- dim = 768 # 假设 dim 已经定义
- new_fields = [
- # 旧字段
- FieldSchema(name="id", dtype=DataType.INT64, is_primary=True),
- FieldSchema(name="title_vector",
- dtype=DataType.FLOAT_VECTOR, dim=dim),
- FieldSchema(name="title", dtype=DataType.VARCHAR, max_length=256,
- description="视频标题"),
- FieldSchema(name="preview_times",
- dtype=DataType.INT64, description="预曝光次数"),
- FieldSchema(name="preview_users", dtype=DataType.INT64,
- description="预曝光用户数"),
- FieldSchema(name="view_times", dtype=DataType.INT64,
- description="曝光次数"),
- FieldSchema(name="view_users", dtype=DataType.INT64,
- description="曝光用户数"),
- FieldSchema(name="play_times", dtype=DataType.INT64,
- description="播放次数"),
- FieldSchema(name="play_users", dtype=DataType.INT64,
- description="播放用户数"),
- FieldSchema(name="share_times", dtype=DataType.INT64,
- description="分享次数"),
- FieldSchema(name="share_users", dtype=DataType.INT64,
- description="分享用户数"),
- FieldSchema(name="return_times",
- dtype=DataType.INT64, description="回看次数"),
- FieldSchema(name="return_users",
- dtype=DataType.INT64, description="回看用户数"),
- # 新字段
- FieldSchema(name="create_time", dtype=DataType.INT64, description="创建时间戳")
- ]
- new_schema = CollectionSchema(new_fields, description="两年内的分发过的视频标题")
- # 创建新 collection
- new_collection_name = "two_year_all_viewed_videos"
- new_collection = Collection(name=new_collection_name, schema=new_schema)
- # 从旧 collection 中读取数据并迁移
- old_collection_name = "two_year_all_video_titles"
- old_collection = Collection(name=old_collection_name)
- old_data = ... # 读取旧 collection 数据的逻辑
- # 为旧数据添加 '创建时间' 字段
- new_data = ...
- for data in old_data:
- data['创建时间'] = ... # 设置 '创建时间' 字段的值
- new_data.append(data)
- # 插入数据到新 collection
- new_collection.insert(new_data)
- # 验证新 collection 数据正确后,可以选择删除旧 collection
- # old_collection.drop()
|