全部產品
Search
文件中心

DashVector:建立Collection

更新時間:Sep 05, 2024

本文介紹如何通過Java SDK建立一個新的Collection。

前提條件

介面定義

// class DashVectorClient

// 通過名稱和向量維度進行建立Collection
public Response<Void> create(String name, int dimension);

// 通過CreateCollectionRequest建立Collection
public Response<Void> create(CreateCollectionRequest request);

使用樣本

說明

需要使用您的api-key替換樣本中的YOUR_API_KEY、您的Cluster Endpoint替換樣本中的YOUR_CLUSTER_ENDPOINT,代碼才能正常運行。

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");

        // 通過CreateCollectionRequest建立Collection
      
        // 建立一個名稱為quickstart、向量維度為4、
        // 向量資料類型為float(預設)、
        // 距離度量方式為dotproduct(內積)的Collection
        // 並預先定義三個Field,名稱為name、weight、age,資料類型分別為str、float、int
        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);
        // example output:
        // {"code":0,"message":"","requestId":"883716a3-32f8-4220-ae54-245fa9b87bf0"}
    }
}

入參描述

可通過CreateCollectionRequestBuilder構造 CreateCollectionRequest對象,其可用方法如下:

方法

必填

預設值

描述

name(String name)

-

待建立的集合名稱

dimension(int dimension)

-

向量維度

dataType(CollectionInfo.DataType dataType)

DataType.FLOAT

向量資料類型,支援

  • DataType.INT

  • DataType.FLOAT

fieldsSchema(Map<String, FieldType> fieldsSchem)

-

Fields定義,Field類型支援

  • FieldType.BOOL

  • FieldType.STRING

  • FieldType.INT

  • FieldType.FLOAT

fieldSchema(String key, FieldType value)

-

metric(CollectionInfo.Metric metric)

Metric.cosine

距離度量支援

  • Metric.cosine

  • Metric.euclidean

  • Metric.dotproduct

metric為cosine時,datatype必須為FLOAT

extraParams(Map<String, String> params)

-

選擇性參數:

timeout(Integer timeout)

-

  • timeout == null:介面開啟同步,待Collection 建立成功後返回

  • timeout == -1:介面開啟非同步

  • timeout >= 0:介面開啟同步並等待,若規定時間Collection未建立成功,則返回逾時

build()

-

-

構造 CreateCollectionRequest對象

說明

出參描述

說明

返回結果為Response<Void>對象,Response<Void>對象中可擷取本次操作結果資訊,如下表所示。

方法

類型

描述

樣本

getCode()

int

傳回值,參考返回狀態代碼說明

0

getMessage()

String

返回訊息

success

getRequestId()

String

請求唯一id

19215409-ea66-4db9-8764-26ce2eb5bb99

isSuccess()

Boolean

判斷請求是否成功

true