createCollection()
A MilvusClient interface. This method creates a collection with the specified schema.
R<RpcStatus> createCollection(CreateCollectionParam requestParam);
CreateCollectionParam
Use the CreateCollectionParam.Builder
to construct a CreateCollectionParam
object.
import io.milvus.param.CreateCollectionParam;
CreateCollectionParam.Builder builder = CreateCollectionParam.newBuilder();
Methods of CreateCollectionParam.Builder
:
Method |
Description |
Parameters |
---|---|---|
withCollectionName(String collectionName) |
Sets the collection name. Collection name cannot be empty or null. |
collectionName: The name of the collection to create. |
withDatabaseName(String databaseName) |
Sets the database name. database name can be null for default database. |
databaseName: The database name. |
withShardsNum(int shardsNum) |
Sets the shards number. The number must be greater or equal to zero. |
shardsNum: The number of shards to split the inserted data into. Multiple shards are processed by multiple nodes in Milvus. |
withDescription(String description) |
Sets the collection description. The description can be empty. The default description is "". |
description: The description of the collection to create. |
withFieldTypes(List<FieldType> fieldTypes) |
Sets the collection schema. The collection schema cannot be empty. |
fieldTypes: a list of FieldType objects, each representing a field schema. |
addFieldType(FieldType fieldType) |
Adds a field schema. |
fieldType: The schema of a field to add in the collection. |
withSchema(CollectionSchemaParam schema) |
Sets the collection schema. It is recommended to use this method instead of withFieldTypes() |
schema: The collection schema |
withConsistencyLevel(ConsistencyLevelEnum consistencyLevel) |
Sets the consistency level. The default value is ConsistencyLevelEnum.BOUNDED |
consistencyLevel: the consistency level of this collection |
withPartitionsNum(int partitionsNum) |
Sets the partitions number if there is partition key field. The number must be greater than zero. |
partitionsNum: Defines the number of partition if there is a partition key field in the collection. |
withReplicaNumber(int replicaNumber) |
Sets the replica number in collection level, then if load collection doesn't have replica number, it will use this replica number. |
replicaNumber: Sets the default replica number for this collection. |
withResourceGroups(List<String> resourceGroups) |
Sets the resource groups in collection level, then if load collection doesn't have resource groups, it will use this resource groups. |
resourceGroups: resource group names |
withProperty(String key, String value) |
Basic method to set a key-value property. |
key: A property key. |
build() |
Constructs a CreateCollectionParam object |
N/A |
The CreateCollectionParam.Builder.build()
can throw the following exceptions:
- ParamException: error if the parameter is invalid.
FieldType
A tool class to represent a field’s schema. Use FieldType.Builder
to build a FieldType
object.
import io.milvus.param.FieldType;
FieldType.Builder builder = FieldType.newBuilder();
FieldType ft = builder.build()
Methods of FieldType.Builder
:
Method |
Description |
Parameters |
---|---|---|
withName(String name) |
Sets the name of the field. The name cannot be empty or null. |
name: The name of the field. |
withPrimaryKey(boolean primaryKey) |
Sets the field as the primary key field. Only fields whose data type is INT64 or VARCHAR can be set as the primary key field. The value is false by default. |
primaryKey: A boolean value that defines if the field is the primary key field. The value true means that the field is the primary key field while the value false means it is not. |
withDescription(String description) |
Sets the field description. The description can be empty. The default value is an empty string. |
description: The description of the field. |
withDataType(DataType dataType) |
Sets the data type for the field. Please refer to DataType in Misc. |
dataType: The data type of the field. |
withElementType(DataType elementType) |
Sets the element type for Array type field. Valid element types for Array: Int8, Int16, Int32, Int64, Varchar, Bool, Float, Double |
elementType: element type of the array. |
addTypeParam(String key, String value) |
Adds a parameter pair for the field. This is mainly used to set extra parameters for the vector field and varchar field. |
key: The parameter key. value: The parameter value. |
withDimension(Integer dimension) |
Sets the dimension of a vector field. The dimension value must be greater than zero. This method internally calls addTypeParam() to store the dimension value. |
dimension: The dimension of the vector field. |
withMaxLength(Integer maxLength) |
Sets the maximum length of a Varchar field. The value must be greater than zero. This method internally calls the addTypeParam() to store the maximum length value. |
maxLength: The maximum length of the varchar field. |
withMaxCapacity(Integer maxCapacity) |
Sets the max capacity of an Array field. The valid capacity value range is [1, 4096] |
maxCapacity: The max capacity of the array. |
withAutoID(boolean autoID) |
Enables auto-ID function for the field. Note that the auto-ID function can only be enabled on primary key field.If auto-ID function is enabled, Milvus automatically generates a unique ID for each entity so that values for the primary key field do not need to be provided during data insertion. If auto-ID is disabled, values for the primary key field need to be provided during data insertion. |
autoID: A boolean value that defines if the primary keys are automatically generated. The value true means that auto-ID is enabled, while the value false means it is not. |
withPartitionKey(boolean partitionKey) |
Sets the field to be partition key.A partition key field's values are hashed and distributed to different logic partitions.Only int64 and varchar type fields can be a partition key. The primary key field can not be a partition key. |
partitionKey: A boolean value that defines if this field is a partition key field. The value true is a partition key, false is not. |
build() |
Create a FieldType object. |
N/A |
CollectionSchemaParam
A tool class to represent a collection’s schema. Use CollectionSchemaParam.Builder
to build a CollectionSchemaParam
object.
import io.milvus.param.collection.CollectionSchemaParam;
CollectionSchemaParam.Builder builder = CollectionSchemaParam.newBuilder();
Methods of CollectionSchemaParam.Builder
:
Method |
Description |
Parameters |
---|---|---|
withEnableDynamicField(boolean enableDynamicField) |
Sets the collection if enableDynamicField. |
enableDynamicField: enableDynamicField of the collection |
withFieldTypes(List<FieldType> fieldTypes) |
Sets the fieldTypes of the schema. The fieldTypes cannot be empty or null. |
fieldTypes: A list of FieldType to defines the fields. |
addFieldType( FieldType fieldType) |
Adds a field schema. |
fieldType: A field schema. |
build() |
Create a CollectionSchemaParam object. |
N/A |
Returns
This method catches all the exceptions and returns an R<RpcStatus>
object.
If the API fails on the server side, it returns the error code and message from the server.
If the API fails by RPC exception, it returns
R.Status.Unknown
and the error message of the exception.If the API succeeds, it returns
R.Status.Success
.
Example
import io.milvus.param.*;
List<FieldType> fieldsSchema = new ArrayList<>();
FieldType field_1 = FieldType.newBuilder()
.withPrimaryKey(true)
.withAutoID(false)
.withDataType(DataType.Int64)
.withName("uid")
.withDescription("unique id")
.build();
fieldsSchema.add(field_1);
FieldType field_2 = FieldType.newBuilder()
.withDataType(DataType.FloatVector)
.withName("embedding")
.withDescription("embeddings")
.withDimension(dimension)
.build();
fieldsSchema.add(field_2);
// create collection
CreateCollectionParam param = CreateCollectionParam.newBuilder()
.withCollectionName(COLLECTION_NAME)
.withDescription("a collection for search")
.withFieldTypes(fieldsSchema)
.build();
R<RpcStatus> response = client.createCollection(param);
if (response.getStatus() != R.Status.Success.getCode()) {
System.out.println(response.getMessage());
}