This topic describes how to create a collection by using the SDK for Java.
Prerequisites
A cluster is created. For more information, see Create a cluster.
An API key is obtained. For more information, see Manage API keys.
The SDK of the latest version is installed. For more information, see Install DashVector SDK.
API definition
// class DashVectorClient
// Create a collection by specifying the name and number of vector dimensions.
public Response<Void> create(String name, int dimension);
// Create a collection by using a CreateCollectionRequest object.
public Response<Void> create(CreateCollectionRequest request);
Example
You need to replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with the endpoint of your cluster in the sample code for the code to run properly.
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 by using a CreateCollectionRequest object.
// Create a collection and set its name to quickstart, the number of vector dimensions to 4,
// vector data type to the default value of DataType.FLOAT,
// and distance metric to dotproduct.
// Predefine the name, weight, and age fields, and set their data types to STRING, FLOAT, and INT, respectively.
CreateCollectionRequest request = CreateCollectionRequest.builder()
.name("quickstart")
.dimension(4)
.metric(CollectionInfo.Metric.dotproduct)
.dataType(CollectionInfo.DataType.FLOAT)
.fieldSchema("name", FieldType.STRING)
.fieldSchema("weight", FieldType.FLOAT)
.fieldSchema("age", FieldType.INT)
.build();
Response<Void> response = client.create(request);
System.out.println(response);
// Output example:
// {"code":0,"message":"","requestId":"883716a3-32f8-4220-ae54-245fa9b87bf0"}
}
}
Request parameters
You can use a CreateCollectionRequestBuilder
instance to build a CreateCollectionRequest
object. The following table describes the methods that can be called by the instance.
Method | Required | Default value | Description |
name(String name) | Yes | - | Sets the name of the collection to be created. |
dimension(int dimension) | Yes | - | Sets the number of vector dimensions. |
dataType(CollectionInfo.DataType dataType) | No | DataType.FLOAT | Sets the vector data type. Valid values:
|
fieldsSchema(Map<String, FieldType> fieldsSchem) | No | - | Sets fields. Valid data types:
|
fieldSchema(String key, FieldType value) | No | - | |
metric(CollectionInfo.Metric metric) | No | Metric.cosine | Sets the distance metric. Valid values:
If the metric is |
timeout(Integer timeout) | No | - |
|
build() | - | - | Builds a |
For more information about the advantages of predefining fields during collection creation, see Schema-free.
Response parameters
A Response<Void>
object is returned, which contains the operation result, as described in the following table.
Method | Type | Description | Example |
getCode() | int | The returned status code. 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 | Specifies whether the operation is successful. | true |