HighLevelExample.java 10 KB

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