Browse Source

fix insertRow err and modify example (#526)

xushuang.hu 2 years ago
parent
commit
9e192f41cd

+ 30 - 28
examples/main/java/io/milvus/GeneralExample.java

@@ -113,22 +113,22 @@ public class GeneralExample {
 //                .withDimension(BINARY_DIM)
 //                .build();
 
-        FieldType fieldType5 = FieldType.newBuilder()
-                .withName(USER_JSON_FIELD)
-                .withDescription("user json")
-                .withDataType(DataType.JSON)
-                .build();
+//        FieldType fieldType5 = FieldType.newBuilder()
+//                .withName(USER_JSON_FIELD)
+//                .withDescription("user json")
+//                .withDataType(DataType.JSON)
+//                .build();
 
         CreateCollectionParam createCollectionReq = CreateCollectionParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .withDescription("customer info")
                 .withShardsNum(2)
-                .withEnableDynamicField(true)
+                .withEnableDynamicField(false)
                 .addFieldType(fieldType1)
                 .addFieldType(fieldType2)
                 .addFieldType(fieldType3)
 //                .addFieldType(fieldType4)
-                .addFieldType(fieldType5)
+//                .addFieldType(fieldType5)
                 .build();
         R<RpcStatus> response = milvusClient.withTimeout(timeoutMilliseconds, TimeUnit.MILLISECONDS)
                 .createCollection(createCollectionReq);
@@ -451,18 +451,18 @@ public class GeneralExample {
             ages.add(ran.nextInt(99));
         }
 
-        List<JSONObject> objectList = new ArrayList<>();
-        for (long i = 0L; i < count ; ++i) {
-            JSONObject obj = new JSONObject();
-            obj.put(INT32_FIELD_NAME, ran.nextInt());
-            obj.put(DOUBLE_FIELD_NAME, ran.nextDouble());
-            objectList.add(obj);
-        }
+//        List<JSONObject> objectList = new ArrayList<>();
+//        for (long i = 0L; i < count ; ++i) {
+//            JSONObject obj = new JSONObject();
+//            obj.put(INT32_FIELD_NAME, ran.nextInt());
+//            obj.put(DOUBLE_FIELD_NAME, ran.nextDouble());
+//            objectList.add(obj);
+//        }
 
         List<InsertParam.Field> fields = new ArrayList<>();
         fields.add(new InsertParam.Field(AGE_FIELD, ages));
         fields.add(new InsertParam.Field(VECTOR_FIELD, vectors));
-        fields.add(new InsertParam.Field(USER_JSON_FIELD, objectList));
+//        fields.add(new InsertParam.Field(USER_JSON_FIELD, objectList));
 //        fields.add(new InsertParam.Field(PROFILE_FIELD, profiles));
 
         InsertParam insertParam = InsertParam.newBuilder()
@@ -487,20 +487,22 @@ public class GeneralExample {
             row.put(VECTOR_FIELD, generateFloatVector());
 
             // $meta if collection EnableDynamicField, you can input this field not exist in schema, else deny
-            row.put(INT32_FIELD_NAME, ran.nextInt());
-            row.put(INT64_FIELD_NAME, ran.nextLong());
-            row.put(VARCHAR_FIELD_NAME, "测试varchar");
-            row.put(FLOAT_FIELD_NAME, ran.nextFloat());
-            row.put(DOUBLE_FIELD_NAME, ran.nextDouble());
-            row.put(BOOL_FIELD_NAME, ran.nextBoolean());
+//            row.put(INT32_FIELD_NAME, ran.nextInt());
+//            row.put(INT64_FIELD_NAME, ran.nextLong());
+//            row.put(VARCHAR_FIELD_NAME, "测试varchar");
+//            row.put(FLOAT_FIELD_NAME, ran.nextFloat());
+//            row.put(DOUBLE_FIELD_NAME, ran.nextDouble());
+//            row.put(BOOL_FIELD_NAME, ran.nextBoolean());
 
             // $json
-            row.put(USER_JSON_FIELD + "[" + INT32_FIELD_NAME + "]", ran.nextInt());
-            row.put(USER_JSON_FIELD + "[" + INT64_FIELD_NAME + "]", ran.nextLong());
-            row.put(USER_JSON_FIELD + "[" + VARCHAR_FIELD_NAME + "]", "测试varchar");
-            row.put(USER_JSON_FIELD + "[" + FLOAT_FIELD_NAME + "]", ran.nextFloat());
-            row.put(USER_JSON_FIELD + "[" + DOUBLE_FIELD_NAME + "]", ran.nextDouble());
-            row.put(USER_JSON_FIELD + "[" + BOOL_FIELD_NAME + "]", ran.nextBoolean());
+//            JSONObject jsonObject = new JSONObject();
+//            jsonObject.put(INT32_FIELD_NAME, ran.nextInt());
+//            jsonObject.put(INT64_FIELD_NAME, ran.nextLong());
+//            jsonObject.put(VARCHAR_FIELD_NAME, "测试varchar");
+//            jsonObject.put(FLOAT_FIELD_NAME, ran.nextFloat());
+//            jsonObject.put(DOUBLE_FIELD_NAME, ran.nextDouble());
+//            jsonObject.put(BOOL_FIELD_NAME, ran.nextBoolean());
+//            row.put(USER_JSON_FIELD, jsonObject);
 
             rowsData.add(row);
         }
@@ -581,7 +583,7 @@ public class GeneralExample {
         }
 
         // insertRows
-        R<MutationResult> result = example.insertRows(partitionName, row_count);
+        R<MutationResult> result = example.insertRows(partitionName, 10);
         MutationResultWrapper wrapper = new MutationResultWrapper(result.getData());
         long insertCount = wrapper.getInsertCount();
         List<Long> longIDs = wrapper.getLongIDs();

+ 15 - 52
src/main/java/io/milvus/param/ParamUtils.java

@@ -283,29 +283,22 @@ public class ParamUtils {
                 InsertDataInfo insertDataInfo = nameInsertInfo.getOrDefault(fieldName, InsertDataInfo.builder()
                         .fieldName(fieldName).dataType(fieldType.getDataType()).data(new LinkedList<>()).build());
 
-                // check jsonField
-                if (fieldType.getDataType() == DataType.JSON) {
-                    JSONObject jsonField = parseJsonData(row, fieldType);
-                    insertDataInfo.getData().add(jsonField);
+                // check normalField
+                Object rowFieldData = row.get(fieldName);
+                if (rowFieldData != null) {
+                    if (fieldType.isAutoID()) {
+                        String msg = "The primary key: " + fieldName + " is auto generated, no need to input.";
+                        throw new ParamException(msg);
+                    }
+                    checkFieldData(fieldType, Lists.newArrayList(rowFieldData));
+
+                    insertDataInfo.getData().add(rowFieldData);
                     nameInsertInfo.put(fieldName, insertDataInfo);
                 } else {
-                    // check normalField
-                    Object rowFieldData = row.get(fieldName);
-                    if (rowFieldData != null) {
-                        if (fieldType.isAutoID()) {
-                            String msg = "The primary key: " + fieldName + " is auto generated, no need to input.";
-                            throw new ParamException(msg);
-                        }
-                        checkFieldData(fieldType, Lists.newArrayList(rowFieldData));
-
-                        insertDataInfo.getData().add(rowFieldData);
-                        nameInsertInfo.put(fieldName, insertDataInfo);
-                    } else {
-                        // check if autoId
-                        if (!fieldType.isAutoID()) {
-                            String msg = "The field: " + fieldType.getName() + " is not provided.";
-                            throw new ParamException(msg);
-                        }
+                    // check if autoId
+                    if (!fieldType.isAutoID()) {
+                        String msg = "The field: " + fieldType.getName() + " is not provided.";
+                        throw new ParamException(msg);
                     }
                 }
             }
@@ -314,21 +307,12 @@ public class ParamUtils {
             if (wrapper.getEnableDynamicField()) {
                 JSONObject dynamicField = new JSONObject();
                 for (String rowFieldName : row.keySet()) {
-                    Pair<String, String> outerJsonField = parseData(rowFieldName);
-                    if (!nameInsertInfo.containsKey(rowFieldName) && outerJsonField == null) {
+                    if (!nameInsertInfo.containsKey(rowFieldName)) {
                         dynamicField.put(rowFieldName, row.get(rowFieldName));
                     }
                 }
                 insertDynamicDataInfo.getData().add(dynamicField);
             }
-
-            for (String rowFieldName : row.keySet()) {
-                Pair<String, String> outerJsonField = parseData(rowFieldName);
-                if (outerJsonField != null  && !nameInsertInfo.containsKey(outerJsonField.getKey())) {
-                    String msg = "The field: " + outerJsonField.getKey() + " not exists, no need to provided.";
-                    throw new ParamException(msg);
-                }
-            }
         }
 
         for (String fieldNameKey : nameInsertInfo.keySet()) {
@@ -340,27 +324,6 @@ public class ParamUtils {
         }
     }
 
-    private static JSONObject parseJsonData(JSONObject row, FieldType fieldType) {
-        JSONObject jsonField = new JSONObject();
-        for (String rowFieldName : row.keySet()) {
-            Pair<String, String> outerJsonField = parseData(rowFieldName);
-            if (outerJsonField != null && fieldType.getName().equals(outerJsonField.getKey())) {
-                jsonField.put(outerJsonField.getValue(), row.get(rowFieldName));
-            }
-        }
-        return jsonField;
-    }
-
-    public static Pair<String, String> parseData(String data) {
-        String pattern = "(\\w+)\\[\"(\\w+)\"\\]";
-        Pattern regex = Pattern.compile(pattern);
-        Matcher matcher = regex.matcher(data);
-        if (matcher.matches()) {
-            return Pair.of(matcher.group(1), matcher.group(2));
-        }
-        return null;
-    }
-
     @SuppressWarnings("unchecked")
     public static SearchRequest convertSearchParam(@NonNull SearchParam requestParam) throws ParamException {
         SearchRequest.Builder builder = SearchRequest.newBuilder()