Browse Source

Add wrapper for DescribeCollectionResponse (#231)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 3 years ago
parent
commit
80fd080d8c

+ 2 - 1
examples/main/io/milvus/GeneralExample.java

@@ -143,7 +143,8 @@ public class GeneralExample {
         R<DescribeCollectionResponse> response = milvusClient.describeCollection(DescribeCollectionParam.newBuilder()
                 .withCollectionName(COLLECTION_NAME)
                 .build());
-        System.out.println(response);
+        DescCollResponseWrapper wrapper = new DescCollResponseWrapper(response.getData());
+        System.out.println(wrapper.toString());
         return response;
     }
 

+ 154 - 0
src/main/java/io/milvus/Response/DescCollResponseWrapper.java

@@ -0,0 +1,154 @@
+package io.milvus.Response;
+
+import io.milvus.grpc.FieldSchema;
+import io.milvus.grpc.KeyValuePair;
+import io.milvus.param.collection.FieldType;
+
+import io.milvus.grpc.CollectionSchema;
+import io.milvus.grpc.DescribeCollectionResponse;
+
+import lombok.NonNull;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Util class to wrap response of <code>DescribeCollection</code> interface.
+ */
+public class DescCollResponseWrapper {
+    private final DescribeCollectionResponse response;
+
+    public DescCollResponseWrapper(@NonNull DescribeCollectionResponse response) {
+        this.response = response;
+    }
+
+    /**
+     * Get name of the collection.
+     *
+     * @return <code>String</code> name of the collection
+     */
+    public String GetCollectionName() {
+        CollectionSchema schema = response.getSchema();
+        return schema.getName();
+    }
+
+    /**
+     * Get description of the collection.
+     *
+     * @return <code>String</code> description of the collection
+     */
+    public String GetCollectionDescription() {
+        CollectionSchema schema = response.getSchema();
+        return schema.getDescription();
+    }
+
+    /**
+     * Get internal id of the collection.
+     *
+     * @return <code>long</code> internal id of the collection
+     */
+    public long GetCollectionID() {
+        return response.getCollectionID();
+    }
+
+    /**
+     * Get shard number of the collection.
+     *
+     * @return <code>int</code> shard number of the collection
+     */
+    public int GetShardNumber() {
+        return response.getShardsNum();
+    }
+
+    /**
+     * Get utc timestamp when collection created.
+     *
+     * @return <code>long</code> utc timestamp when collection created
+     */
+    public long GetCreatedUtcTimestamp() {
+        return response.getCreatedUtcTimestamp();
+    }
+
+    /**
+     * Get aliases of the collection.
+     *
+     * @return <code>List<String></String></code> aliases of the collection
+     */
+    public List<String> GetAliases() {
+        List<String> aliases = new ArrayList<>();
+        for (int i = 0; i < response.getAliasesCount(); ++i) {
+            aliases.add(response.getAliases(i));
+        }
+
+        return aliases;
+    }
+
+    /**
+     * Get schema of the collection's fields.
+     *
+     * @return <code>List<FieldType></code> schema of the collection's fields
+     */
+    public List<FieldType> GetFields() {
+        List<FieldType> results = new ArrayList<>();
+        CollectionSchema schema = response.getSchema();
+        List<FieldSchema> fields = schema.getFieldsList();
+        fields.forEach((field) -> results.add(convertField(field)));
+
+        return results;
+    }
+
+    /**
+     * Get schema of a field by name.
+     * Return null if the field doesn't exist
+     *
+     * @return <code>FieldType</code> schema of the field
+     */
+    public FieldType GetField(@NonNull String name) {
+        CollectionSchema schema = response.getSchema();
+        for (int i = 0; i < schema.getFieldsCount(); ++i) {
+            FieldSchema field = schema.getFields(i);
+            if (name.compareTo(field.getName()) == 0) {
+                return convertField(field);
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Convert a grpc field schema to client schema
+     *
+     * @return <code>FieldType</code> schema of the field
+     */
+    private FieldType convertField(@NonNull FieldSchema field) {
+        FieldType.Builder builder = FieldType.newBuilder()
+                .withName(field.getName())
+                .withDescription(field.getDescription())
+                .withPrimaryKey(field.getIsPrimaryKey())
+                .withAutoID(field.getAutoID())
+                .withDataType(field.getDataType());
+
+        List<KeyValuePair> keyValuePairs = field.getTypeParamsList();
+        keyValuePairs.forEach((kv) -> builder.addTypeParam(kv.getKey(), kv.getValue()));
+
+        return builder.build();
+    }
+
+    /**
+     * Construct a <code>String</code> by <code>DescCollResponseWrapper</code> instance.
+     *
+     * @return <code>String</code>
+     */
+    @Override
+    public String toString() {
+        return "Collection Description{" +
+                "name:'" + GetCollectionName() + '\'' +
+                ", description:'" + GetCollectionDescription() + '\'' +
+                ", id:" + GetCollectionID() +
+                ", shardNumber:" + GetShardNumber() +
+                ", createdUtcTimestamp:" + GetCreatedUtcTimestamp() +
+                ", aliases:" + GetAliases() +
+                ", fields:" + GetFields().toString() +
+                '}';
+    }
+}

+ 1 - 1
src/main/java/io/milvus/param/collection/CreateCollectionParam.java

@@ -154,7 +154,7 @@ public class CreateCollectionParam {
                 "collectionName='" + collectionName + '\'' +
                 ", shardsNum=" + shardsNum +
                 ", description='" + description + '\'' +
-                ", field count=" + fieldTypes.size() +
+                ", fields=" + fieldTypes.toString() +
                 '}';
     }
 }

+ 16 - 0
src/main/java/io/milvus/param/collection/FieldType.java

@@ -187,4 +187,20 @@ public class FieldType {
             return new FieldType(this);
         }
     }
+
+    /**
+     * Construct a <code>String</code> by <code>FieldType</code> instance.
+     *
+     * @return <code>String</code>
+     */
+    @Override
+    public String toString() {
+        return "FieldType{" +
+                "name='" + name + '\'' +
+                ", type='" + dataType.name() + '\'' +
+                ", primaryKey=" + primaryKey +
+                ", autoID=" + autoID +
+                ", params=" + typeParams.toString() +
+                '}';
+    }
 }

+ 7 - 0
src/test/java/io/milvus/client/MilvusClientDockerTest.java

@@ -218,6 +218,13 @@ public class MilvusClientDockerTest {
         R<RpcStatus> createR = client.createCollection(createParam);
         assertEquals(createR.getStatus().intValue(), R.Status.Success.getCode());
 
+        R<DescribeCollectionResponse> response = client.describeCollection(DescribeCollectionParam.newBuilder()
+                .withCollectionName(randomCollectionName)
+                .build());
+
+        DescCollResponseWrapper desc = new DescCollResponseWrapper(response.getData());
+        System.out.println(desc.toString());
+
         // insert data
         int rowCount = 10000;
         List<Long> ids = new ArrayList<>();