-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathBulkWriterVolumeExample.java
More file actions
655 lines (588 loc) · 30.3 KB
/
BulkWriterVolumeExample.java
File metadata and controls
655 lines (588 loc) · 30.3 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
* 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.bulkwriter;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import io.milvus.bulkwriter.VolumeBulkWriter;
import io.milvus.bulkwriter.VolumeBulkWriterParam;
import io.milvus.bulkwriter.common.clientenum.BulkFileType;
import io.milvus.bulkwriter.common.utils.GeneratorUtils;
import io.milvus.bulkwriter.model.UploadFilesResult;
import io.milvus.bulkwriter.request.describe.CloudDescribeImportRequest;
import io.milvus.bulkwriter.request.import_.VolumeImportRequest;
import io.milvus.bulkwriter.request.list.CloudListImportJobsRequest;
import io.milvus.bulkwriter.restful.BulkImportUtils;
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.*;
import io.milvus.v2.service.index.request.CreateIndexReq;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.response.QueryResp;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class BulkWriterVolumeExample {
private static final Gson GSON_INSTANCE = new Gson();
// milvus
public static final String HOST = "127.0.0.1";
public static final Integer PORT = 19530;
public static final String USER_NAME = "user.name";
public static final String PASSWORD = "password";
/**
* The value of the URL is fixed.
* For overseas regions, it is: https://api.cloud.zilliz.com
* For regions in China, it is: https://api.cloud.zilliz.com.cn
*/
public static final String CLOUD_ENDPOINT = "https://api.cloud.zilliz.com";
public static final String API_KEY = "_api_key_for_cluster_org_";
public static final String VOLUME_NAME = "_volume_name_for_project_";
public static final String CLUSTER_ID = "_your_cloud_cluster_id_";
// If db_name is not specified, use ""
public static final String DB_NAME = "";
public static final String COLLECTION_NAME = "_collection_name_on_the_db_";
// If partition_name is not specified, use ""
public static final String PARTITION_NAME = "_partition_name_on_the_collection_";
private static final Integer DIM = 512;
private static final Integer ARRAY_CAPACITY = 10;
private static MilvusClientV2 milvusClient;
public static void main(String[] args) throws Exception {
createConnection();
exampleCollectionRemoteVolume(BulkFileType.PARQUET);
}
private static void createConnection() {
System.out.println("\nCreate connection...");
String url = String.format("http://%s:%s", HOST, PORT);
milvusClient = new MilvusClientV2(ConnectConfig.builder()
.uri(url)
.username(USER_NAME)
.password(PASSWORD)
.build());
System.out.println("\nConnected");
}
private static void exampleCollectionRemoteVolume(BulkFileType fileType) throws Exception {
List<Map<String, Object>> originalData = genOriginalData(5);
List<JsonObject> rows = genImportData(originalData, true);
// 4 types vectors + all scalar types + dynamic field enabled, use cloud import api.
// You need to apply a cloud service from Zilliz Cloud(https://zilliz.com/cloud)
CreateCollectionReq.CollectionSchema collectionSchema = buildAllTypesSchema();
createCollection(COLLECTION_NAME, collectionSchema, false);
UploadFilesResult uploadFilesResult = volumeRemoteWriter(collectionSchema, fileType, rows);
callVolumeImport(uploadFilesResult.getVolumeName(), uploadFilesResult.getPath());
verifyImportData(collectionSchema, originalData);
}
private static void callVolumeImport(String volumeName, String path) throws InterruptedException {
List<String> importDataPath = Lists.newArrayList(path);
VolumeImportRequest volumeImportRequest = VolumeImportRequest.builder()
.apiKey(API_KEY)
.volumeName(volumeName).dataPaths(Lists.newArrayList(Collections.singleton(importDataPath)))
.clusterId(CLUSTER_ID).dbName(DB_NAME).collectionName(COLLECTION_NAME).partitionName(PARTITION_NAME)
.build();
String bulkImportResult = BulkImportUtils.bulkImport(CLOUD_ENDPOINT, volumeImportRequest);
System.out.println(bulkImportResult);
JsonObject bulkImportObject = convertJsonObject(bulkImportResult);
String jobId = bulkImportObject.getAsJsonObject("data").get("jobId").getAsString();
System.out.println("Create a cloudImport job, job id: " + jobId);
System.out.println("\n===================== call cloudListImportJobs ====================");
CloudListImportJobsRequest listImportJobsRequest = CloudListImportJobsRequest.builder().clusterId(CLUSTER_ID).currentPage(1).pageSize(10).apiKey(API_KEY).build();
String listImportJobsResult = BulkImportUtils.listImportJobs(CLOUD_ENDPOINT, listImportJobsRequest);
System.out.println(listImportJobsResult);
while (true) {
System.out.println("Wait 5 second to check bulkInsert job state...");
TimeUnit.SECONDS.sleep(5);
System.out.println("\n===================== call cloudGetProgress ====================");
CloudDescribeImportRequest request = CloudDescribeImportRequest.builder().clusterId(CLUSTER_ID).jobId(jobId).apiKey(API_KEY).build();
String getImportProgressResult = BulkImportUtils.getImportProgress(CLOUD_ENDPOINT, request);
JsonObject getImportProgressObject = convertJsonObject(getImportProgressResult);
String importProgressState = getImportProgressObject.getAsJsonObject("data").get("state").getAsString();
String progress = getImportProgressObject.getAsJsonObject("data").get("progress").getAsString();
if ("Failed".equals(importProgressState)) {
String reason = getImportProgressObject.getAsJsonObject("data").get("reason").getAsString();
System.out.printf("The job %s failed, reason: %s%n", jobId, reason);
break;
} else if ("Completed".equals(importProgressState)) {
System.out.printf("The job %s completed%n", jobId);
break;
} else {
System.out.printf("The job %s is running, state:%s progress:%s%n", jobId, importProgressState, progress);
}
}
}
private static List<Map<String, Object>> genOriginalData(int count) {
List<Map<String, Object>> data = new ArrayList<>();
for (int i = 0; i < count; ++i) {
Map<String, Object> row = new HashMap<>();
// scalar field
row.put("id", (long) i);
row.put("bool", i % 5 == 0);
row.put("int8", i % 128);
row.put("int16", i % 1000);
row.put("int32", i % 100000);
row.put("float", (float) i / 3);
row.put("double", (double) i / 7);
row.put("varchar", "varchar_" + i);
row.put("json", String.format("{\"dummy\": %s, \"ok\": \"name_%s\"}", i, i));
// vector field
row.put("float_vector", CommonUtils.generateFloatVector(DIM));
row.put("binary_vector", CommonUtils.generateBinaryVector(DIM).array());
row.put("float16_vector", CommonUtils.generateFloat16Vector(DIM, false).array());
row.put("sparse_vector", CommonUtils.generateSparseVector());
// array field
row.put("array_bool", GeneratorUtils.generatorBoolValue(3));
row.put("array_int8", GeneratorUtils.generatorInt8Value(4));
row.put("array_int16", GeneratorUtils.generatorInt16Value(5));
row.put("array_int32", GeneratorUtils.generatorInt32Value(6));
row.put("array_int64", GeneratorUtils.generatorLongValue(7));
row.put("array_varchar", GeneratorUtils.generatorVarcharValue(8, 10));
row.put("array_float", GeneratorUtils.generatorFloatValue(9));
row.put("array_double", GeneratorUtils.generatorDoubleValue(10));
data.add(row);
}
// a special record with null/default values
{
Map<String, Object> row = new HashMap<>();
// scalar field
row.put("id", (long) data.size());
row.put("bool", null);
row.put("int8", null);
row.put("int16", 16);
row.put("int32", null);
row.put("float", null);
row.put("double", null);
row.put("varchar", null);
row.put("json", null);
// vector field
row.put("float_vector", CommonUtils.generateFloatVector(DIM));
row.put("binary_vector", CommonUtils.generateBinaryVector(DIM).array());
row.put("float16_vector", CommonUtils.generateFloat16Vector(DIM, false).array());
row.put("sparse_vector", CommonUtils.generateSparseVector());
// array field
row.put("array_bool", GeneratorUtils.generatorBoolValue(10));
row.put("array_int8", GeneratorUtils.generatorInt8Value(9));
row.put("array_int16", null);
row.put("array_int32", GeneratorUtils.generatorInt32Value(7));
row.put("array_int64", GeneratorUtils.generatorLongValue(6));
row.put("array_varchar", GeneratorUtils.generatorVarcharValue(5, 10));
row.put("array_float", GeneratorUtils.generatorFloatValue(4));
row.put("array_double", null);
data.add(row);
}
return data;
}
private static List<JsonObject> genImportData(List<Map<String, Object>> originalData, boolean isEnableDynamicField) {
List<JsonObject> data = new ArrayList<>();
for (Map<String, Object> row : originalData) {
JsonObject rowObject = new JsonObject();
// scalar field
rowObject.addProperty("id", (Number) row.get("id"));
if (row.get("bool") != null) { // nullable value can be missed
rowObject.addProperty("bool", (Boolean) row.get("bool"));
}
rowObject.addProperty("int8", row.get("int8") == null ? null : (Number) row.get("int8"));
rowObject.addProperty("int16", row.get("int16") == null ? null : (Number) row.get("int16"));
rowObject.addProperty("int32", row.get("int32") == null ? null : (Number) row.get("int32"));
rowObject.addProperty("float", row.get("float") == null ? null : (Number) row.get("float"));
if (row.get("double") != null) { // nullable value can be missed
rowObject.addProperty("double", (Number) row.get("double"));
}
rowObject.addProperty("varchar", row.get("varchar") == null ? null : (String) row.get("varchar"));
// Note: for JSON field, use gson.fromJson() to construct a real JsonObject
// don't use rowObject.addProperty("json", jsonContent) since the value is treated as a string, not a JsonObject
Object jsonContent = row.get("json");
rowObject.add("json", jsonContent == null ? null : GSON_INSTANCE.fromJson((String) jsonContent, JsonElement.class));
// vector field
rowObject.add("float_vector", GSON_INSTANCE.toJsonTree(row.get("float_vector")));
rowObject.add("binary_vector", GSON_INSTANCE.toJsonTree(row.get("binary_vector")));
rowObject.add("float16_vector", GSON_INSTANCE.toJsonTree(row.get("float16_vector")));
rowObject.add("sparse_vector", GSON_INSTANCE.toJsonTree(row.get("sparse_vector")));
// array field
rowObject.add("array_bool", GSON_INSTANCE.toJsonTree(row.get("array_bool")));
rowObject.add("array_int8", GSON_INSTANCE.toJsonTree(row.get("array_int8")));
rowObject.add("array_int16", GSON_INSTANCE.toJsonTree(row.get("array_int16")));
rowObject.add("array_int32", GSON_INSTANCE.toJsonTree(row.get("array_int32")));
rowObject.add("array_int64", GSON_INSTANCE.toJsonTree(row.get("array_int64")));
rowObject.add("array_varchar", GSON_INSTANCE.toJsonTree(row.get("array_varchar")));
rowObject.add("array_float", GSON_INSTANCE.toJsonTree(row.get("array_float")));
rowObject.add("array_double", GSON_INSTANCE.toJsonTree(row.get("array_double")));
// dynamic fields
if (isEnableDynamicField) {
rowObject.addProperty("dynamic", "dynamic_" + row.get("id"));
}
data.add(rowObject);
}
return data;
}
private static UploadFilesResult volumeRemoteWriter(CreateCollectionReq.CollectionSchema collectionSchema,
BulkFileType fileType,
List<JsonObject> data) throws Exception {
System.out.printf("\n===================== all field types (%s) ====================%n", fileType.name());
try (VolumeBulkWriter volumeBulkWriter = buildVolumeBulkWriter(collectionSchema, fileType)) {
for (JsonObject rowObject : data) {
volumeBulkWriter.appendRow(rowObject);
}
System.out.printf("%s rows appends%n", volumeBulkWriter.getTotalRowCount());
System.out.println("Generate data files...");
volumeBulkWriter.commit(false);
UploadFilesResult uploadResult = volumeBulkWriter.getVolumeUploadResult();
System.out.printf("Data files have been uploaded: %s%n", uploadResult);
return uploadResult;
} catch (Exception e) {
System.out.println("allTypesRemoteWriter catch exception: " + e);
throw e;
}
}
private static VolumeBulkWriter buildVolumeBulkWriter(CreateCollectionReq.CollectionSchema collectionSchema, BulkFileType fileType) throws IOException {
VolumeBulkWriterParam bulkWriterParam = VolumeBulkWriterParam.newBuilder()
.withCollectionSchema(collectionSchema)
.withRemotePath("bulk_data")
.withFileType(fileType)
.withChunkSize(512 * 1024 * 1024)
.withConfig("sep", "|") // only take effect for CSV file
.withCloudEndpoint(CLOUD_ENDPOINT)
.withApiKey(API_KEY)
.withVolumeName(VOLUME_NAME)
.build();
return new VolumeBulkWriter(bulkWriterParam);
}
/**
* @param collectionSchema collection info
* @param dropIfExist if collection already exist, will drop firstly and then create again
*/
private static void createCollection(String collectionName, CreateCollectionReq.CollectionSchema collectionSchema, boolean dropIfExist) {
System.out.println("\n===================== create collection ====================");
checkMilvusClientIfExist();
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName(collectionName)
.collectionSchema(collectionSchema)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
Boolean has = milvusClient.hasCollection(HasCollectionReq.builder().collectionName(collectionName).build());
if (has) {
if (dropIfExist) {
milvusClient.dropCollection(DropCollectionReq.builder().collectionName(collectionName).build());
milvusClient.createCollection(requestCreate);
}
} else {
milvusClient.createCollection(requestCreate);
}
System.out.printf("Collection %s created%n", collectionName);
}
private static void comparePrint(CreateCollectionReq.CollectionSchema collectionSchema,
Map<String, Object> expectedData, Map<String, Object> fetchedData,
String fieldName) {
CreateCollectionReq.FieldSchema field = collectionSchema.getField(fieldName);
Object expectedValue = expectedData.get(fieldName);
if (expectedValue == null) {
if (field.getDefaultValue() != null) {
expectedValue = field.getDefaultValue();
// for Int8/Int16 value, the default value is Short type, the returned value is Integer type
if (expectedValue instanceof Short) {
expectedValue = ((Short) expectedValue).intValue();
}
}
}
Object fetchedValue = fetchedData.get(fieldName);
if (fetchedValue == null || fetchedValue instanceof JsonNull) {
if (!field.getIsNullable()) {
throw new RuntimeException("Field is not nullable but fetched data is null");
}
if (expectedValue != null) {
throw new RuntimeException("Expected value is not null but fetched data is null");
}
return; // both fetchedValue and expectedValue are null
}
boolean matched;
if (fetchedValue instanceof Float) {
matched = Math.abs((Float) fetchedValue - (Float) expectedValue) < 1e-4;
} else if (fetchedValue instanceof Double) {
matched = Math.abs((Double) fetchedValue - (Double) expectedValue) < 1e-8;
} else if (fetchedValue instanceof JsonElement) {
JsonElement expectedJson = GSON_INSTANCE.fromJson((String) expectedValue, JsonElement.class);
matched = fetchedValue.equals(expectedJson);
} else if (fetchedValue instanceof ByteBuffer) {
byte[] bb = ((ByteBuffer) fetchedValue).array();
matched = Arrays.equals(bb, (byte[]) expectedValue);
} else if (fetchedValue instanceof List) {
matched = fetchedValue.equals(expectedValue);
// currently, for array field, null value, the server returns an empty list
if (((List<?>) fetchedValue).isEmpty() && expectedValue == null) {
matched = true;
}
} else {
matched = fetchedValue.equals(expectedValue);
}
if (!matched) {
System.out.print("Fetched value:");
System.out.println(fetchedValue);
System.out.print("Expected value:");
System.out.println(expectedValue);
throw new RuntimeException("Fetched data is unmatched");
}
}
private static void verifyImportData(CreateCollectionReq.CollectionSchema collectionSchema, List<Map<String, Object>> rows) {
createIndex();
List<Long> QUERY_IDS = Lists.newArrayList(1L, (long) rows.get(rows.size() - 1).get("id"));
System.out.printf("Load collection and query items %s%n", QUERY_IDS);
loadCollection();
String expr = String.format("id in %s", QUERY_IDS);
System.out.println(expr);
List<QueryResp.QueryResult> results = query(expr, Lists.newArrayList("*"));
System.out.println("Verify data...");
if (results.size() != QUERY_IDS.size()) {
throw new RuntimeException("Result count is incorrect");
}
for (QueryResp.QueryResult result : results) {
Map<String, Object> fetchedEntity = result.getEntity();
long id = (Long) fetchedEntity.get("id");
Map<String, Object> originalEntity = rows.get((int) id);
comparePrint(collectionSchema, originalEntity, fetchedEntity, "bool");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "int8");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "int16");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "int32");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "float");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "double");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "varchar");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "json");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_bool");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_int8");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_int16");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_int32");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_int64");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_varchar");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_float");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "array_double");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "float_vector");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "binary_vector");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "float16_vector");
comparePrint(collectionSchema, originalEntity, fetchedEntity, "sparse_vector");
System.out.println(fetchedEntity);
}
System.out.println("Result is correct!");
}
private static void createIndex() {
System.out.println("Create index...");
checkMilvusClientIfExist();
List<IndexParam> indexes = new ArrayList<>();
indexes.add(IndexParam.builder()
.fieldName("float_vector")
.indexType(IndexParam.IndexType.FLAT)
.metricType(IndexParam.MetricType.L2)
.build());
indexes.add(IndexParam.builder()
.fieldName("binary_vector")
.indexType(IndexParam.IndexType.BIN_FLAT)
.metricType(IndexParam.MetricType.HAMMING)
.build());
indexes.add(IndexParam.builder()
.fieldName("float16_vector")
.indexType(IndexParam.IndexType.FLAT)
.metricType(IndexParam.MetricType.IP)
.build());
indexes.add(IndexParam.builder()
.fieldName("sparse_vector")
.indexType(IndexParam.IndexType.SPARSE_WAND)
.metricType(IndexParam.MetricType.IP)
.build());
milvusClient.createIndex(CreateIndexReq.builder()
.collectionName(COLLECTION_NAME)
.indexParams(indexes)
.build());
milvusClient.loadCollection(LoadCollectionReq.builder()
.collectionName(COLLECTION_NAME)
.build());
}
private static void loadCollection() {
System.out.println("Refresh load collection...");
checkMilvusClientIfExist();
// RefreshLoad is a new interface from v2.5.3,
// mainly used when there are new segments generated by bulkinsert request,
// force the new segments to be loaded into memory.
milvusClient.refreshLoad(RefreshLoadReq.builder()
.collectionName(COLLECTION_NAME)
.build());
System.out.println("Collection row number: " + getCollectionRowCount());
}
private static List<QueryResp.QueryResult> query(String expr, List<String> outputFields) {
System.out.println("========== query() ==========");
checkMilvusClientIfExist();
QueryReq test = QueryReq.builder()
.collectionName(COLLECTION_NAME)
.filter(expr)
.outputFields(outputFields)
.build();
QueryResp response = milvusClient.query(test);
return response.getQueryResults();
}
private static Long getCollectionRowCount() {
System.out.println("========== getCollectionRowCount() ==========");
checkMilvusClientIfExist();
// Get row count, set ConsistencyLevel.STRONG to sync the data to query node so that data is visible
QueryResp countR = milvusClient.query(QueryReq.builder()
.collectionName(COLLECTION_NAME)
.filter("")
.outputFields(Collections.singletonList("count(*)"))
.consistencyLevel(ConsistencyLevel.STRONG)
.build());
return (long) countR.getQueryResults().get(0).getEntity().get("count(*)");
}
private static CreateCollectionReq.CollectionSchema buildAllTypesSchema() {
CreateCollectionReq.CollectionSchema schemaV2 = CreateCollectionReq.CollectionSchema.builder()
.enableDynamicField(true)
.build();
// scalar field
schemaV2.addField(AddFieldReq.builder()
.fieldName("id")
.dataType(DataType.Int64)
.isPrimaryKey(Boolean.TRUE)
.autoID(false)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("bool")
.dataType(DataType.Bool)
.isNullable(true)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("int8")
.dataType(DataType.Int8)
.defaultValue((short) 88)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("int16")
.dataType(DataType.Int16)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("int32")
.dataType(DataType.Int32)
.isNullable(true)
.defaultValue(999999)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("float")
.dataType(DataType.Float)
.isNullable(true)
.defaultValue((float) 3.14159)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("double")
.dataType(DataType.Double)
.isNullable(true)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("varchar")
.dataType(DataType.VarChar)
.maxLength(512)
.isNullable(true)
.defaultValue("this is default value")
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("json")
.dataType(DataType.JSON)
.isNullable(true)
.build());
// vector fields
schemaV2.addField(AddFieldReq.builder()
.fieldName("float_vector")
.dataType(DataType.FloatVector)
.dimension(DIM)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("binary_vector")
.dataType(DataType.BinaryVector)
.dimension(DIM)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("float16_vector")
.dataType(DataType.Float16Vector)
.dimension(DIM)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("sparse_vector")
.dataType(DataType.SparseFloatVector)
.build());
// array fields
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_bool")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Bool)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_int8")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Int8)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_int16")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Int16)
.isNullable(true)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_int32")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Int32)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_int64")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Int64)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_varchar")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.VarChar)
.maxLength(512)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_float")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Float)
.build());
schemaV2.addField(AddFieldReq.builder()
.fieldName("array_double")
.dataType(DataType.Array)
.maxCapacity(ARRAY_CAPACITY)
.elementType(DataType.Double)
.isNullable(true)
.build());
return schemaV2;
}
private static void checkMilvusClientIfExist() {
if (milvusClient == null) {
String msg = "milvusClient is null. Please initialize it by calling createConnection() first before use.";
throw new RuntimeException(msg);
}
}
private static JsonObject convertJsonObject(String result) {
return GSON_INSTANCE.fromJson(result, JsonObject.class);
}
}