HighLevelExample.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. package io.milvus.v1;
  20. import com.google.gson.Gson;
  21. import com.google.gson.JsonObject;
  22. import com.google.common.collect.Lists;
  23. import io.milvus.client.MilvusServiceClient;
  24. import io.milvus.common.clientenum.ConsistencyLevelEnum;
  25. import io.milvus.common.utils.JacksonUtils;
  26. import io.milvus.common.utils.VectorUtils;
  27. import io.milvus.grpc.*;
  28. import io.milvus.param.*;
  29. import io.milvus.param.collection.*;
  30. import io.milvus.param.highlevel.collection.response.ListCollectionsResponse;
  31. import io.milvus.param.highlevel.collection.CreateSimpleCollectionParam;
  32. import io.milvus.param.highlevel.collection.ListCollectionsParam;
  33. import io.milvus.param.highlevel.dml.*;
  34. import io.milvus.param.highlevel.dml.response.*;
  35. import io.milvus.response.*;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.Random;
  39. public class HighLevelExample {
  40. private static final MilvusServiceClient milvusClient;
  41. static {
  42. ConnectParam connectParam = ConnectParam.newBuilder()
  43. .withHost("localhost")
  44. .withPort(19530)
  45. .withAuthorization("root","Milvus")
  46. .build();
  47. milvusClient = new MilvusServiceClient(connectParam);
  48. }
  49. private static final String COLLECTION_NAME = "java_sdk_example_highlevel_v1";
  50. private static final String ID_FIELD = "userID";
  51. private static final String VECTOR_FIELD = "userFace";
  52. private static final String USER_JSON_FIELD = "userJson";
  53. private static final Integer VECTOR_DIM = 36;
  54. private static final String AGE_FIELD = "userAge";
  55. private static final String INDEX_NAME = "userFaceIndex";
  56. private static final IndexType INDEX_TYPE = IndexType.IVF_FLAT;
  57. private static final String INDEX_PARAM = "{\"nlist\":128}";
  58. private static final String INT32_FIELD_NAME = "int32";
  59. private static final String INT64_FIELD_NAME = "int64";
  60. private static final String VARCHAR_FIELD_NAME = "varchar";
  61. private static final String BOOL_FIELD_NAME = "bool";
  62. private static final String FLOAT_FIELD_NAME = "float";
  63. private static final String DOUBLE_FIELD_NAME = "double";
  64. private R<DescribeCollectionResponse> describeCollection() {
  65. System.out.println("========== describeCollection() ==========");
  66. R<DescribeCollectionResponse> response = milvusClient.describeCollection(DescribeCollectionParam.newBuilder()
  67. .withCollectionName(COLLECTION_NAME)
  68. .build());
  69. CommonUtils.handleResponseStatus(response);
  70. DescCollResponseWrapper wrapper = new DescCollResponseWrapper(response.getData());
  71. System.out.println(wrapper);
  72. return response;
  73. }
  74. // >>>>>>>>>>>>> high level api
  75. private R<RpcStatus> createCollection() {
  76. System.out.println("========== high level createCollection ==========");
  77. CreateSimpleCollectionParam createSimpleCollectionParam = CreateSimpleCollectionParam.newBuilder()
  78. .withCollectionName(COLLECTION_NAME)
  79. .withDimension(VECTOR_DIM)
  80. .withPrimaryField(ID_FIELD)
  81. .withVectorField(VECTOR_FIELD)
  82. .withAutoId(true)
  83. .build();
  84. R<RpcStatus> response = milvusClient.createCollection(createSimpleCollectionParam);
  85. CommonUtils.handleResponseStatus(response);
  86. System.out.println(JacksonUtils.toJsonString(response.getData()));
  87. return response;
  88. }
  89. private R<ListCollectionsResponse> listCollections() {
  90. System.out.println("========== high level listCollections ==========");
  91. ListCollectionsParam listCollectionsParam = ListCollectionsParam.newBuilder()
  92. .build();
  93. R<ListCollectionsResponse> response = milvusClient.listCollections(listCollectionsParam);
  94. CommonUtils.handleResponseStatus(response);
  95. System.out.println(response);
  96. return response;
  97. }
  98. private R<InsertResponse> insertRows(int rowCount) {
  99. System.out.println("========== high level insertRows ==========");
  100. List<JsonObject> rowsData = new ArrayList<>();
  101. Random ran = new Random();
  102. Gson gson = new Gson();
  103. for (long i = 0L; i < rowCount; ++i) {
  104. JsonObject row = new JsonObject();
  105. row.addProperty(AGE_FIELD, ran.nextInt(99));
  106. row.add(VECTOR_FIELD, gson.toJsonTree(CommonUtils.generateFloatVector(VECTOR_DIM)));
  107. // $meta if collection EnableDynamicField, you can input this field not exist in schema, else deny
  108. row.addProperty(INT32_FIELD_NAME, ran.nextInt());
  109. row.addProperty(INT64_FIELD_NAME, ran.nextLong());
  110. row.addProperty(VARCHAR_FIELD_NAME, String.format("varchar_%d", i));
  111. row.addProperty(FLOAT_FIELD_NAME, ran.nextFloat());
  112. row.addProperty(DOUBLE_FIELD_NAME, ran.nextDouble());
  113. row.addProperty(BOOL_FIELD_NAME, ran.nextBoolean());
  114. // $json
  115. JsonObject jsonObject = new JsonObject();
  116. jsonObject.addProperty(INT32_FIELD_NAME, ran.nextInt());
  117. jsonObject.addProperty(INT64_FIELD_NAME, ran.nextLong());
  118. jsonObject.addProperty(VARCHAR_FIELD_NAME, String.format("varchar_%d", i));
  119. jsonObject.addProperty(FLOAT_FIELD_NAME, ran.nextFloat());
  120. jsonObject.addProperty(DOUBLE_FIELD_NAME, ran.nextDouble());
  121. jsonObject.addProperty(BOOL_FIELD_NAME, ran.nextBoolean());
  122. row.add(USER_JSON_FIELD, jsonObject);
  123. rowsData.add(row);
  124. }
  125. InsertRowsParam insertRowsParam = InsertRowsParam.newBuilder()
  126. .withCollectionName(COLLECTION_NAME)
  127. .withRows(rowsData)
  128. .build();
  129. R<InsertResponse> response = milvusClient.insert(insertRowsParam);
  130. CommonUtils.handleResponseStatus(response);
  131. System.out.println("insertCount: " + response.getData().getInsertCount());
  132. System.out.println("insertIds: " + response.getData().getInsertIds());
  133. return response;
  134. }
  135. private R<DeleteResponse> delete(List<?> ids) {
  136. System.out.println("========== high level insertRows ==========");
  137. DeleteIdsParam deleteIdsParam = DeleteIdsParam.newBuilder()
  138. .withCollectionName(COLLECTION_NAME)
  139. .withPrimaryIds(ids)
  140. .build();
  141. R<DeleteResponse> response = milvusClient.delete(deleteIdsParam);
  142. CommonUtils.handleResponseStatus(response);
  143. System.out.println(response);
  144. return response;
  145. }
  146. private R<GetResponse> get(List<?> ids) {
  147. System.out.println("========== high level get ==========");
  148. GetIdsParam getParam = GetIdsParam.newBuilder()
  149. .withCollectionName(COLLECTION_NAME)
  150. .withPrimaryIds(ids)
  151. .build();
  152. R<GetResponse> response = milvusClient.get(getParam);
  153. CommonUtils.handleResponseStatus(response);
  154. for (QueryResultsWrapper.RowRecord rowRecord : response.getData().getRowRecords()) {
  155. System.out.println(rowRecord);
  156. }
  157. return response;
  158. }
  159. private R<SearchResponse> searchSimple(String filter) {
  160. System.out.println("========== high level search ==========");
  161. SearchSimpleParam searchSimpleParam = SearchSimpleParam.newBuilder()
  162. .withCollectionName(COLLECTION_NAME)
  163. .withVectors(CommonUtils.generateFloatVector(VECTOR_DIM))
  164. .withFilter(filter)
  165. .withLimit(100L)
  166. .withOffset(0L)
  167. .withOutputFields(Lists.newArrayList("int32", "int64"))
  168. .withConsistencyLevel(ConsistencyLevelEnum.STRONG)
  169. .build();
  170. R<SearchResponse> response = milvusClient.search(searchSimpleParam);
  171. CommonUtils.handleResponseStatus(response);
  172. for (QueryResultsWrapper.RowRecord rowRecord : response.getData().getRowRecords(0)) {
  173. System.out.println(rowRecord);
  174. }
  175. return response;
  176. }
  177. private R<QueryResponse> querySimple(String filter) {
  178. milvusClient.flush(FlushParam.newBuilder().addCollectionName(COLLECTION_NAME).build());
  179. System.out.println("========== high level query ==========");
  180. QuerySimpleParam querySimpleParam = QuerySimpleParam.newBuilder()
  181. .withCollectionName(COLLECTION_NAME)
  182. .withFilter(filter)
  183. .withOutputFields(Lists.newArrayList("int32", "int64"))
  184. .withLimit(100L)
  185. .withOffset(0L)
  186. .withConsistencyLevel(ConsistencyLevelEnum.STRONG)
  187. .build();
  188. R<QueryResponse> response = milvusClient.query(querySimpleParam);
  189. CommonUtils.handleResponseStatus(response);
  190. for (QueryResultsWrapper.RowRecord rowRecord : response.getData().getRowRecords()) {
  191. System.out.println(rowRecord);
  192. }
  193. return response;
  194. }
  195. public static void main(String[] args) {
  196. HighLevelExample example = new HighLevelExample();
  197. example.createCollection();
  198. example.listCollections();
  199. R<DescribeCollectionResponse> describeCollectionResponseR = example.describeCollection();
  200. DescCollResponseWrapper descCollResponseWrapper = new DescCollResponseWrapper(describeCollectionResponseR.getData());
  201. int dataCount = 5;
  202. R<InsertResponse> insertResponse = example.insertRows(dataCount);
  203. List<?> insertIds = insertResponse.getData().getInsertIds();
  204. example.get(insertIds);
  205. String expr = VectorUtils.convertPksExpr(insertIds, descCollResponseWrapper);
  206. example.querySimple(expr);
  207. example.searchSimple(expr);
  208. // Asynchronous deletion. A successful response does not guarantee immediate data deletion. Please wait for a certain period of time for the deletion operation to take effect.
  209. example.delete(insertIds);
  210. }
  211. }