Skip to content

Commit

Permalink
create_milvus_collection
Browse files Browse the repository at this point in the history
  • Loading branch information
big-cir committed Oct 19, 2022
1 parent 8219216 commit 6860c91
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
Binary file modified .DS_Store
Binary file not shown.
92 changes: 92 additions & 0 deletions ex_create_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: daewon
"""

from pymilvus import connections, utility, Collection, CollectionSchema, FieldSchema, DataType
import os, glob, random
import numpy as np

img_path = sorted(glob.glob("./mongt/save/*.jpg"))

file_box = []
for i in img_path :
filename = i.split("/")[3]
file_box.append(filename)

# milvus connection
connections.connect()

# select exist collections()
utility.list_collections()

# define field in collection
# p_id = FieldSchema(name, dtype, kwargs)
p_id = FieldSchema("id",
dtype = DataType.INT64,
is_primary = True
)

field1 = FieldSchema("test_col",
dtype = DataType.VARCHAR,
max_length = 200,
)

field2 = FieldSchema("test_col2",
dtype = DataType.FLOAT_VECTOR,
dim = 2
)


test_schema = CollectionSchema(fields = [p_id, field1, field2])

# define collection name
collection_name = "test5"

# create collection
collection = Collection(name = collection_name,
schema = test_schema,
using = 'default',
shards_num = 3)

# load collection
collection = Collection(collection_name)

# check exist collections
utility.list_collections()

# define data, insert into collection
data = [ [i for i in range(161)],
[str(i) for i in file_box],
[[random.random() for _ in range(2)] for _ in range(161)], #[i for i in ar],
]

arr2 = [[random.random() for _ in range(2)] for _ in range(161)]
np.ndim(arr2)

# 컬렉션에 data 삽입
collection.insert(data)

# 현재 collection load
collection = Collection(collection_name)
collection.load()

# Query 조회 -> parameter expr: boolean
# boolean expr rules -> documents 참고
res = collection.query(
expr = "id < %d" % len(file_box),
output_fields = ["id", "test_col", "test_col2"]
)

# default id로 오름차순 정렬
sorted_res = sorted(res, key=lambda k: k["id"])

# select한 data출력
sorted_res

d_str = ["mil", "vs", "test"]
data2 = [ [i for i in range(3)],
[str(i) for i in d_str],
[[random.random() for _ in range(2)] for _ in range(2)]
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Getter
@NoArgsConstructor
@Document(collection = "")
@Document(collection = "kts_biblio")
public class DownFileCollection {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
@Service
@RequiredArgsConstructor
public class DownFileService {
private final String downPath = "";
private final String downPath = "/home/master/Desktop/milvus_test/save_img";
// "/Users/daewon/Desktop/mongt/save/";

public void getCollection(int i, Mono<DownFileCollection> getObject) {
Object sampleImageInfoArray = getObject.block().getSampleImageInfoArray();
Expand Down

0 comments on commit 6860c91

Please sign in to comment.