All Products
Search
Document Center

AnalyticDB:Java

Last Updated:Nov 26, 2025

OpenAPI encapsulates Data Definition Language (DDL) and Data Manipulation Language (DML) operations for vectors in AnalyticDB for PostgreSQL. This lets you manage vector data using OpenAPI. This topic describes how to import and query vector data by calling operations in the Java software development kit (SDK).

Prerequisites

Procedure

  1. Initialize the vector database.

  2. Create a namespace.

  3. Create a collection.

  4. Upload vector data.

  5. Retrieve vector data.

Initialize the vector database

Before you use vector search, you must initialize the knowledgebase database and the full-text index feature.

The following example shows how to call an API operation:

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

For more information about the parameters, see InitVectorDatabase - Initialize a vector database.

Create a namespace

Namespaces are used for schema isolation. Before you use vectors, you must create at least one namespace or use the public namespace.

The following example shows how to call an API operation:

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

For more information about the parameters, see CreateNamespace - Create a namespace.

After the namespace is created, you can view the corresponding schema in the knowledgebase database of the instance.

SELECT schema_name FROM information_schema.schemata;

Create a collection

Collections store vector data and are isolated by namespaces.

The following example shows a sample call:

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_cn");
request.setRegionId("ap-southeast-1");
CreateCollectionResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));

For more information about the parameters, see CreateCollection - Create a vector data set.

After the collection is created, you can view the corresponding table in the knowledgebase database of the instance.

SELECT tablename FROM pg_tables WHERE schemaname='vector_test';

Upload vector data

Upload the prepared embedding vector data to the corresponding collection.

The following example shows how to call an API operation:

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", "Test document");
rowsMetadata.put("content","Test 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));

For more information about the parameters, see UpsertCollectionData - Upload vector data.

After the upload is complete, you can view the data in the knowledgebase database of the instance.

SELECT * FROM vector_test.document;

Retrieve vector data

Prepare the query vectors or full-text index fields, and then call the query API operation.

The following example shows how to call an API operation:

QueryCollectionDataRequest request = new QueryCollectionDataRequest();
request.setDBInstanceId("gp-bp1c62r3l489****");
request.setCollection("document");
request.setNamespace("vector_test");
request.setNamespacePassword("vector_test_password");
request.setContent("Test");
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));

The following result is returned:

{
  "Matches": {
    "match": [
      {
        "Id": "0CB55798-ECF5-4064-B81E-FE35B19E01A6",
        "Metadata": {
          "title":"Test document",
          "content":"Test 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"
}

References

Vector search