forked from milvus-io/milvus-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpsertExample.java
More file actions
169 lines (152 loc) · 6.58 KB
/
UpsertExample.java
File metadata and controls
169 lines (152 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.milvus.v2;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.milvus.v1.CommonUtils;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.ConsistencyLevel;
import io.milvus.v2.common.DataType;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.DropCollectionReq;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.request.UpsertReq;
import io.milvus.v2.service.vector.response.InsertResp;
import io.milvus.v2.service.vector.response.QueryResp;
import io.milvus.v2.service.vector.response.UpsertResp;
import java.util.*;
public class UpsertExample {
private static final MilvusClientV2 client;
static {
client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
}
private static final String COLLECTION_NAME = "java_sdk_example_upsert_v2";
private static final String ID_FIELD = "pk";
private static final String VECTOR_FIELD = "vector";
private static final String TEXT_FIELD = "text";
private static final Integer VECTOR_DIM = 128;
private static List<Object> createCollection(boolean autoID) {
// Drop collection if exists
client.dropCollection(DropCollectionReq.builder()
.collectionName(COLLECTION_NAME)
.build());
// Create collection
CreateCollectionReq.CollectionSchema collectionSchema = CreateCollectionReq.CollectionSchema.builder()
.build();
collectionSchema.addField(AddFieldReq.builder()
.fieldName(ID_FIELD)
.dataType(DataType.Int64)
.isPrimaryKey(Boolean.TRUE)
.autoID(autoID)
.build());
collectionSchema.addField(AddFieldReq.builder()
.fieldName(VECTOR_FIELD)
.dataType(DataType.FloatVector)
.dimension(VECTOR_DIM)
.build());
collectionSchema.addField(AddFieldReq.builder()
.fieldName(TEXT_FIELD)
.dataType(DataType.VarChar)
.maxLength(100)
.build());
List<IndexParam> indexes = new ArrayList<>();
indexes.add(IndexParam.builder()
.fieldName(VECTOR_FIELD)
.indexType(IndexParam.IndexType.FLAT)
.metricType(IndexParam.MetricType.COSINE)
.build());
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName(COLLECTION_NAME)
.collectionSchema(collectionSchema)
.indexParams(indexes)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
client.createCollection(requestCreate);
System.out.println("\nCollection created with autoID = " + autoID);
// Insert rows
Gson gson = new Gson();
List<JsonObject> rows = new ArrayList<>();
for (int i = 0; i < 100; i++) {
JsonObject row = new JsonObject();
if (!autoID) {
row.addProperty(ID_FIELD, i);
}
List<Float> vector = CommonUtils.generateFloatVector(VECTOR_DIM);
row.add(VECTOR_FIELD, gson.toJsonTree(vector));
row.addProperty(TEXT_FIELD, String.format("text_%d", i));
rows.add(row);
}
InsertResp resp = client.insert(InsertReq.builder()
.collectionName(COLLECTION_NAME)
.data(rows)
.build());
return resp.getPrimaryKeys();
}
private static void queryWithExpr(String expr) {
QueryResp queryRet = client.query(QueryReq.builder()
.collectionName(COLLECTION_NAME)
.filter(expr)
.outputFields(Arrays.asList(ID_FIELD, TEXT_FIELD))
.consistencyLevel(ConsistencyLevel.STRONG)
.build());
System.out.println("\nQuery with expression: " + expr);
List<QueryResp.QueryResult> records = queryRet.getQueryResults();
for (QueryResp.QueryResult record : records) {
System.out.println(record.getEntity());
}
}
private static void doUpsert(boolean autoID) {
// if autoID is true, the collection primary key is auto-generated by server
List<Object> ids = createCollection(autoID);
// query before upsert
Long testID = (Long)ids.get(1);
String filter = String.format("%s == %d", ID_FIELD, testID);
queryWithExpr(filter);
// upsert
// the server will return a new primary key, the old entity is deleted,
// and a new entity is created with the new primary key
Gson gson = new Gson();
JsonObject row = new JsonObject();
row.addProperty(ID_FIELD, testID);
List<Float> vector = CommonUtils.generateFloatVector(VECTOR_DIM);
row.add(VECTOR_FIELD, gson.toJsonTree(vector));
row.addProperty(TEXT_FIELD, "this field has been updated");
UpsertResp upsertResp = client.upsert(UpsertReq.builder()
.collectionName(COLLECTION_NAME)
.data(Collections.singletonList(row))
.build());
List<Object> newIds = upsertResp.getPrimaryKeys();
Long newID = (Long)newIds.get(0);
System.out.println("\nUpsert done");
// query after upsert
filter = String.format("%s == %d", ID_FIELD, newID);
queryWithExpr(filter);
}
public static void main(String[] args) {
doUpsert(true);
doUpsert(false);
client.close();
}
}