|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.milvus.v1; |
| 21 | + |
| 22 | +import com.google.gson.Gson; |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import io.milvus.client.MilvusClient; |
| 25 | +import io.milvus.client.MilvusServiceClient; |
| 26 | +import io.milvus.common.clientenum.ConsistencyLevelEnum; |
| 27 | +import io.milvus.grpc.DataType; |
| 28 | +import io.milvus.grpc.MutationResult; |
| 29 | +import io.milvus.grpc.QueryResults; |
| 30 | +import io.milvus.param.*; |
| 31 | +import io.milvus.param.collection.*; |
| 32 | +import io.milvus.param.dml.InsertParam; |
| 33 | +import io.milvus.param.dml.QueryParam; |
| 34 | +import io.milvus.param.dml.UpsertParam; |
| 35 | +import io.milvus.param.index.CreateIndexParam; |
| 36 | +import io.milvus.response.QueryResultsWrapper; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +public class UpsertExample { |
| 44 | + private static final MilvusClient client; |
| 45 | + |
| 46 | + static { |
| 47 | + ConnectParam connectParam = ConnectParam.newBuilder() |
| 48 | + .withHost("localhost") |
| 49 | + .withPort(19530) |
| 50 | + .build(); |
| 51 | + client = new MilvusServiceClient(connectParam); |
| 52 | + } |
| 53 | + private static final String COLLECTION_NAME = "java_sdk_example_upsert_v1"; |
| 54 | + private static final String ID_FIELD = "pk"; |
| 55 | + private static final String VECTOR_FIELD = "vector"; |
| 56 | + private static final String TEXT_FIELD = "text"; |
| 57 | + private static final Integer VECTOR_DIM = 128; |
| 58 | + |
| 59 | + private static void queryWithExpr(String expr) { |
| 60 | + R<QueryResults> queryRet = client.query(QueryParam.newBuilder() |
| 61 | + .withCollectionName(COLLECTION_NAME) |
| 62 | + .withExpr(expr) |
| 63 | + .withOutFields(Arrays.asList(ID_FIELD, TEXT_FIELD)) |
| 64 | + .withConsistencyLevel(ConsistencyLevelEnum.STRONG) |
| 65 | + .build()); |
| 66 | + QueryResultsWrapper queryWrapper = new QueryResultsWrapper(queryRet.getData()); |
| 67 | + System.out.println("\nQuery with expression: " + expr); |
| 68 | + List<QueryResultsWrapper.RowRecord> records = queryWrapper.getRowRecords(); |
| 69 | + for (QueryResultsWrapper.RowRecord record : records) { |
| 70 | + System.out.println(record); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static List<Long> createCollection(boolean autoID) { |
| 75 | + // Define fields |
| 76 | + List<FieldType> fieldsSchema = Arrays.asList( |
| 77 | + FieldType.newBuilder() |
| 78 | + .withName(ID_FIELD) |
| 79 | + .withDataType(DataType.Int64) |
| 80 | + .withPrimaryKey(true) |
| 81 | + .withAutoID(autoID) |
| 82 | + .build(), |
| 83 | + FieldType.newBuilder() |
| 84 | + .withName(VECTOR_FIELD) |
| 85 | + .withDataType(DataType.FloatVector) |
| 86 | + .withDimension(VECTOR_DIM) |
| 87 | + .build(), |
| 88 | + FieldType.newBuilder() |
| 89 | + .withName(TEXT_FIELD) |
| 90 | + .withDataType(DataType.VarChar) |
| 91 | + .withMaxLength(100) |
| 92 | + .build() |
| 93 | + ); |
| 94 | + |
| 95 | + CollectionSchemaParam collectionSchemaParam = CollectionSchemaParam.newBuilder() |
| 96 | + .withFieldTypes(fieldsSchema) |
| 97 | + .build(); |
| 98 | + |
| 99 | + // Drop the collection if exists |
| 100 | + client.dropCollection(DropCollectionParam.newBuilder() |
| 101 | + .withCollectionName(COLLECTION_NAME) |
| 102 | + .build()); |
| 103 | + |
| 104 | + // Create the collection with 3 fields |
| 105 | + R<RpcStatus> ret = client.createCollection(CreateCollectionParam.newBuilder() |
| 106 | + .withCollectionName(COLLECTION_NAME) |
| 107 | + .withSchema(collectionSchemaParam) |
| 108 | + .build()); |
| 109 | + CommonUtils.handleResponseStatus(ret); |
| 110 | + |
| 111 | + // Specify an index type on the vector field. |
| 112 | + ret = client.createIndex(CreateIndexParam.newBuilder() |
| 113 | + .withCollectionName(COLLECTION_NAME) |
| 114 | + .withFieldName(VECTOR_FIELD) |
| 115 | + .withIndexType(IndexType.FLAT) |
| 116 | + .withMetricType(MetricType.L2) |
| 117 | + .build()); |
| 118 | + CommonUtils.handleResponseStatus(ret); |
| 119 | + |
| 120 | + // Call loadCollection() to enable automatically loading data into memory for searching |
| 121 | + client.loadCollection(LoadCollectionParam.newBuilder() |
| 122 | + .withCollectionName(COLLECTION_NAME) |
| 123 | + .build()); |
| 124 | + System.out.println("\nCollection created with autoID = " + autoID); |
| 125 | + |
| 126 | + // insert rows |
| 127 | + Gson gson = new Gson(); |
| 128 | + List<JsonObject> rows = new ArrayList<>(); |
| 129 | + for (int i = 0; i < 100; i++) { |
| 130 | + JsonObject row = new JsonObject(); |
| 131 | + if (!autoID) { |
| 132 | + row.addProperty(ID_FIELD, i); |
| 133 | + } |
| 134 | + List<Float> vector = CommonUtils.generateFloatVector(VECTOR_DIM); |
| 135 | + row.add(VECTOR_FIELD, gson.toJsonTree(vector)); |
| 136 | + row.addProperty(TEXT_FIELD, String.format("text_%d", i)); |
| 137 | + rows.add(row); |
| 138 | + } |
| 139 | + R<MutationResult> resp = client.insert(InsertParam.newBuilder() |
| 140 | + .withCollectionName(COLLECTION_NAME) |
| 141 | + .withRows(rows) |
| 142 | + .build()); |
| 143 | + CommonUtils.handleResponseStatus(resp); |
| 144 | + return resp.getData().getIDs().getIntId().getDataList(); |
| 145 | + } |
| 146 | + |
| 147 | + private static void doUpsert(boolean autoID) { |
| 148 | + // if autoID is true, the collection primary key is auto-generated by server |
| 149 | + List<Long> ids = createCollection(autoID); |
| 150 | + |
| 151 | + // query before upsert |
| 152 | + Long testID = ids.get(1); |
| 153 | + String filter = String.format("%s == %d", ID_FIELD, testID); |
| 154 | + queryWithExpr(filter); |
| 155 | + |
| 156 | + // upsert |
| 157 | + // the server will return a new primary key, the old entity is deleted, |
| 158 | + // and a new entity is created with the new primary key |
| 159 | + Gson gson = new Gson(); |
| 160 | + JsonObject row = new JsonObject(); |
| 161 | + row.addProperty(ID_FIELD, testID); |
| 162 | + List<Float> vector = CommonUtils.generateFloatVector(VECTOR_DIM); |
| 163 | + row.add(VECTOR_FIELD, gson.toJsonTree(vector)); |
| 164 | + row.addProperty(TEXT_FIELD, "this field has been updated"); |
| 165 | + R<MutationResult> upsertResp = client.upsert(UpsertParam.newBuilder() |
| 166 | + .withCollectionName(COLLECTION_NAME) |
| 167 | + .withRows(Collections.singletonList(row)) |
| 168 | + .build()); |
| 169 | + CommonUtils.handleResponseStatus(upsertResp); |
| 170 | + List<Long> newIds = upsertResp.getData().getIDs().getIntId().getDataList(); |
| 171 | + Long newID = newIds.get(0); |
| 172 | + System.out.println("\nUpsert done"); |
| 173 | + |
| 174 | + // query after upsert |
| 175 | + filter = String.format("%s == %d", ID_FIELD, newID); |
| 176 | + queryWithExpr(filter); |
| 177 | + } |
| 178 | + |
| 179 | + public static void main(String[] args) { |
| 180 | + doUpsert(true); |
| 181 | + doUpsert(false); |
| 182 | + |
| 183 | + client.close(); |
| 184 | + } |
| 185 | +} |
0 commit comments