全部產品
Search
文件中心

AnalyticDB:Java

更新時間:Jun 19, 2024

OpenAPI封裝了雲原生資料倉儲AnalyticDB PostgreSQL版向量操作的DDL和DML,使您可以通過OpenAPI來管理向量資料。本文以SDK Java調用方式介紹如何通過API匯入並查詢向量資料。

前提條件

操作流程

  1. 初始化向量庫

  2. 建立Namespace

  3. 建立Collection

  4. 上傳向量資料

  5. 召迴向量資料

初始化向量庫

在使用向量檢索前,需初始化knowledgebase庫以及全文檢索索引相關功能。

調用樣本如下:

InitVectorDatabaseRequest request = new InitVectorDatabaseRequest();
request.setDBInstanceId("gp-bp1c62r3l489****");
request.setManagerAccount("myaccount");
request.setManagerAccountPassword("myaccount_password");
request.setRegionId("ap-southeast-1");
InitVectorDatabaseResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));

參數說明,請參見InitVectorDatabase - 初始化向量資料庫

建立Namespace

Namespace用於Schema隔離,在使用向量前,需至少建立一個Namespace或者使用public的Namespace。

調用樣本如下:

CreateNamespaceRequest request = new CreateNamespaceRequest();
request.setDBInstanceId("gp-bp1c62r3l489****");
request.setManagerAccount("myaccount");
request.setManagerAccountPassword("myaccount_password");
request.setNamespace("vector_test");
request.setNamespacePassword("vector_test_password");
request.setRegionId("ap-southeast-1");
CreateNamespaceResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));

參數說明,請參見CreateNamespace - 建立命名空間

建立完後,可以在執行個體的knowledgebase庫查看對應的Schema。

SELECT schema_name FROM information_schema.schemata;

建立Collection

Collection用於儲存向量資料,並使用Namespace隔離。

調用樣本如下:

Map<String,String> metadata = new HashMap<>();
metadata.put("title", "text");
metadata.put("link", "text");
metadata.put("content", "text");
metadata.put("pv", "int");
List<String> fullTextRetrievalFields = Arrays.asList("title", "content");

CreateCollectionRequest request = new CreateCollectionRequest();
request.setDBInstanceId("gp-bp1c62r3l489****");
request.setManagerAccount("myaccount");
request.setManagerAccountPassword("myaccount_password");
request.setNamespace("vector_test");
request.setCollection("document");
request.setDimension(10L);
request.setFullTextRetrievalFields(StringUtils.join(fullTextRetrievalFields, ","));
request.setMetadata(new Gson().toJson(metadata));
request.setParser("zh_ch");
request.setRegionId("ap-southeast-1");
CreateCollectionResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));

參數說明,請參見CreateCollection - 建立向量資料集

建立完後,可以在執行個體的knowledgebase庫查看對應的Table。

SELECT tablename FROM pg_tables WHERE schemaname='vector_test';

上傳向量資料

將準備好的Embedding向量資料上傳到對應的Collection中。

調用樣本如下:

UpsertCollectionDataRequest request = new UpsertCollectionDataRequest();
request.setDBInstanceId("gp-bp1c62r3l489****");
request.setCollection("document");
request.setNamespace("vector_test");
request.setNamespacePassword("vector_test_password");
request.setRegionId("ap-southeast-1");

List<UpsertCollectionDataRequest.UpsertCollectionDataRequestRows> rows = new ArrayList<>();
UpsertCollectionDataRequest.UpsertCollectionDataRequestRows row = new UpsertCollectionDataRequest.UpsertCollectionDataRequestRows();
row.setId("0CB55798-ECF5-4064-B81E-FE35B19E01A6");
row.setVector(Arrays.asList(0.2894745251078251,0.5364747050266715,0.1276845661831275,0.22528871956822372,0.7009319238651552,0.40267406135256123,0.8873626696379067,0.1248525955774931,0.9115507046412368,0.2450859133174706));
Map<String, String> rowsMetadata = new HashMap<>();
rowsMetadata.put("title", "測試文檔");
rowsMetadata.put("content","測試內容");
rowsMetadata.put("link","http://127.0.0.1/document1");
rowsMetadata.put("pv","1000");
row.setMetadata(rowsMetadata);
rows.add(row);
request.setRows(rows);
UpsertCollectionDataResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));

參數說明,請參見UpsertCollectionData - 上傳向量資料

上傳完成,可以在執行個體的knowledgebase庫查看資料。

SELECT * FROM vector_test.document;

召迴向量資料

準備需要召回的查詢向量或全文檢索索引欄位,執行查詢介面。

調用樣本如下:

QueryCollectionDataRequest request = new QueryCollectionDataRequest();
request.setDBInstanceId("gp-bp1c62r3l489****");
request.setCollection("document");
request.setNamespace("vector_test");
request.setNamespacePassword("vector_test_password");
request.setContent("測試");
request.setFilter("pv > 10");
request.setTopK(10L);
request.setVector(Arrays.asList(0.7152607422256894,0.5524872066437732,0.1168505269851303,0.704130971473022,0.4118874999967596,0.2451574619214022,0.18193414783144812,0.3050522957905741,0.24846180714868163,0.0549715380856951));
request.setRegionId("ap-southeast-1");
QueryCollectionDataResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));

返回結果如下:

{
  "Matches": {
    "match": [
      {
        "Id": "0CB55798-ECF5-4064-B81E-FE35B19E01A6",
        "Metadata": {
          "title":"測試文檔",
          "content":"測試內容",
          "link":"http://127.0.0.1/document1",
          "pv":"1000"
        },
        "Values": [
           0.2894745251078251,
           0.5364747050266715,
           0.1276845661831275,
           0.22528871956822372,
           0.7009319238651552,
           0.40267406135256123,
           0.8873626696379067,
           0.1248525955774931,
           0.9115507046412368,
           0.2450859133174706
        ]
      }
    ]
  },
  "RequestId": "ABB39CC3-4488-4857-905D-2E4A051D0521",
  "Status": "success"
}

相關文檔

向量檢索