This topic describes how to create a collection using the Java SDK.
Prerequisites
A cluster has been created. For more information, see Create a cluster.
An API key has been obtained. For more information, see Manage API keys.
The latest version of the software development kit (SDK) has been installed. For more information, see Install the DashVector SDK.
API definition
// class DashVectorClient
// Create a collection by specifying a name and vector dimensions.
public Response<Void> create(String name, int dimension);
// Create a collection using a CreateCollectionRequest object.
public Response<Void> create(CreateCollectionRequest request);Examples
To run the code, replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with your cluster endpoint.
Create a single-vector collection
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.common.DashVectorException;
import com.aliyun.dashvector.models.requests.CreateCollectionRequest;
import com.aliyun.dashvector.models.responses.Response;
import com.aliyun.dashvector.proto.CollectionInfo;
import com.aliyun.dashvector.proto.FieldType;
public class Main {
public static void main(String[] args) throws DashVectorException {
DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");
// Create a collection using a CreateCollectionRequest object.
// Create a collection named quickstart with 4 vector dimensions,
// a vector data type of float (the default),
// and a distance metric of dotproduct.
// Predefine multiple fields: name (STRING), weight (FLOAT), age (INT), id (LONG), tags (ARRAY_STRING), numbers (ARRAY_INT), grades (ARRAY_FLOAT), and bankCards (ARRAY_LONG).
CreateCollectionRequest request = CreateCollectionRequest.builder()
.name("quickstart")
.dimension(4)
.metric(CollectionInfo.Metric.dotproduct)
.dataType(CollectionInfo.DataType.FLOAT)
.filedSchema("name", FieldType.STRING)
.filedSchema("weight", FieldType.FLOAT)
.filedSchema("age", FieldType.INT)
.filedSchema("id", FieldType.LONG)
.filedSchema("tags", FieldType.ARRAY_STRING)
.filedSchema("numbers", FieldType.ARRAY_INT)
.filedSchema("grades", FieldType.ARRAY_FLOAT)
.filedSchema("bankCards", FieldType.ARRAY_LONG)
.build();
Response<Void> response = client.create(request);
System.out.println(response);
// example output:
// {"code":0,"message":"","requestId":"883716a3-32f8-4220-ae54-245fa9b87bf0"}
}
}Create a multi-vector collection
public void createCollection() {
CreateCollectionRequest createCollectionRequest = CreateCollectionRequest.builder()
.name("multi_vector_demo")
.vectors("title", VectorParam.builder().dimension(4).quantizeType("DT_VECTOR_INT8").build())
.vectors("content", VectorParam.builder().dimension(6).metric(CollectionInfo.Metric.euclidean).build())
.sparseVectors("abstruct", VectorParam.builder().metric(CollectionInfo.Metric.dotproduct).build())
.sparseVectors("keywords", VectorParam.builder().metric(CollectionInfo.Metric.dotproduct).build())
// Sparse vector indexes currently support only the dotproduct metric. You do not need to set the dimension or dtype because the default values are used.
.filedSchema("author", FieldType.STRING)
.filedSchema("tags", FieldType.ARRAY_STRING)
.build();
Response<Void> createResponse = client.create(createCollectionRequest);
System.out.println(createResponse);
assert createResponse.isSuccess();
}Request parameters
Use CreateCollectionRequestBuilder to construct a CreateCollectionRequest object. The following table describes the available methods.
Method | Required | Default value | Description |
name(String name) | Yes | - | The name of the collection to create. |
dimension(int dimension) | Yes | - | The vector dimensions. |
dataType(CollectionInfo.DataType dataType) | No | DataType.FLOAT | Supported vector data types
|
fieldsSchema(Map<String, FieldType> fieldsSchem) | No | - | The field definitions. The following field types are supported:
|
fieldSchema(String key, FieldType value) | No | - | |
metric(CollectionInfo.Metric metric) | No | Metric.cosine | Supported distance measures
If you set the metric to |
extraParams(Map<String, String> params) | No | - | Optional parameters:
|
timeout(Integer timeout) | No | - |
|
vectorParam(VectorParam) | No | - | Sets advanced parameters for the vector field, such as enabling quantization. For more information, see VectorParam. |
vectors(Map<String, VectorParam>) | No | - | Defines a multi-vector collection. For more information, see Multi-vector search. |
sparseVectors(Map<String, VectorParam>) | No | - | Defines a sparse multi-vector collection. For more information, see Multi-vector search. |
build() | - | - | Constructs a |
For more information about the benefits of predefining fields when you create a collection, see Schema-free.
For more information about quantization policies, see Dynamic vector quantization.
Response parameters
The operation returns a Response<Void> object. The Response<Void> object contains information about the result of the operation, as described in the following table.
Method | Type | Description | Example |
getCode() | int | The return value. For more information, see Status codes. | 0 |
getMessage() | String | The returned message. | success |
getRequestId() | String | The unique ID of the request. | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
isSuccess() | Boolean | Indicates whether the request was successful. | true |