本文介紹如何通過Java SDK建立一個新的Collection。
前提條件
已建立Cluster:建立Cluster。
已獲得API-KEY:API-KEY管理。
已安裝最新版SDK:安裝DashVector SDK。
介面定義
// 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 | 向量資料類型,支援
|
fieldsSchema(Map<String, FieldType> fieldsSchem) | 否 | - | Fields定義,Field類型支援
|
fieldSchema(String key, FieldType value) | 否 | - | |
metric(CollectionInfo.Metric metric) | 否 | Metric.cosine | 距離度量支援
metric為 |
extraParams(Map<String, String> params) | 否 | - | 選擇性參數:
|
timeout(Integer timeout) | 否 | - |
|
build() | - | - | 構造 |
建立Collection時預先定義Fields的收益見Schema Free
量化策略詳情可參考向量動態量化
出參描述
返回結果為Response<Void>對象,Response<Void>對象中可擷取本次操作結果資訊,如下表所示。
方法 | 類型 | 描述 | 樣本 |
getCode() | int | 傳回值,參考返回狀態代碼說明 | 0 |
getMessage() | String | 返回訊息 | success |
getRequestId() | String | 請求唯一id | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
isSuccess() | Boolean | 判斷請求是否成功 | true |