All Products
Search
Document Center

Vector Retrieval Service:Create a collection

Last Updated:Sep 05, 2024

This topic describes how to create a collection by using the SDK for Java.

Prerequisites

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

Note

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:

  • DataType.INT

  • DataType.FLOAT

fieldsSchema(Map<String, FieldType> fieldsSchem)

No

-

Sets fields. Valid data types:

  • FieldType.BOOL

  • FieldType.STRING

  • FieldType.INT

  • FieldType.FLOAT

fieldSchema(String key, FieldType value)

No

-

metric(CollectionInfo.Metric metric)

No

Metric.cosine

Sets the distance metric. Valid values:

  • Metric.cosine

  • Metric.euclidean

  • Metric.dotproduct

If the metric is cosine, the datatype() method must set the field data type to FLOAT.

timeout(Integer timeout)

No

-

  • Sets the timeout period. If you set timeout to null, the method works in synchronous mode and returns a response when the collection is successfully created.

  • If you set timeout to -1, the method works in asynchronous mode.

  • If you set timeout to a value greater than or equal to 0, the method works in synchronous mode and waits for the operation result within the specified timeout period. The method returns a timeout error if the collection fails to be created within the specified timeout period.

build()

-

-

Builds a CreateCollectionRequest object.

Note

For more information about the advantages of predefining fields during collection creation, see Schema-free.

Response parameters

Note

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