Update data

更新时间:
复制 MD 格式

This article describes how to use an API to add and delete data in a table.

Step 1: Add a table

To import a full dataset, create a table and configure a data source such as OSS, MaxCompute, or Data Lake Formation (DLF).

In the left-side navigation pane of the Instance Details page, click Table Management and then click Add Table.

For more information on creating a table, see the following topics:

After you configure the table, you can use an API to insert data once its status changes to In Use.

In the table list, the Status column shows In Use.

Step 2: Configure the public access whitelist

If you are accessing the instance from the same VPC and vSwitch, you can skip this step.

If you are accessing the instance from an on-premises environment or the public internet, see Configure the public access whitelist.

Step 3: Push data to a table

The following example uses Python.

Install the dependency:

pip install alibabacloud-ha3engine-vector

Sample code:

# -*- coding: utf-8 -*-

from alibabacloud_ha3engine_vector import models, client
from Tea.exceptions import TeaException, RetryError

Config = models.Config(
    endpoint="<your_api_endpoint>",        # The API endpoint of your instance. You can find this on the Instance Details page under API Endpoint. 
                                        # Remove the "http://" prefix.
                                        # For on-premises or public internet access, use the public endpoint.
                                        # For access within the same VPC, use the private endpoint.
    instance_id="<your_instance_id>",      # The instance ID. You can find this in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605
    protocol="http",
    access_user_name="<your_username>",    # The username for API access. You can find this on the Instance Details page under API Endpoint.
    access_pass_word="<your_password>"     # The password for the username. You can manage this on the Instance Details page under API Endpoint.

)

# Initialize the engine client.
ha3EngineClient = client.Client(Config)

def push():
    # The table name for pushing documents must be in the format _.
    tableName = "<instance_id>_<table_name>";

    try:
        # Add a document. This is an upsert operation: if a document with the same primary key exists, 
        # it will be overwritten; otherwise, a new document is added.
        # =====================================================
        # Define the fields for the document.
        add2DocumentFields = {
            "id": 1,                          # Primary key (INT).
            "cate_id": "123",                 # Single-value field (STRING).
            "vector": "a\x1Db\x1Dc\x1Dd"      # Multi-value field (STRING). Values are separated by the \x1D character.
        }
        
        # Structure the document for the request.
        add2Document = {
            "fields": add2DocumentFields,
            "cmd": "add"                      # Use the "add" command to insert or update a document.
        }

        optionsHeaders = {}
        # The request body is an array of one or more document operations.
        documentArrayList = []
        documentArrayList.append(add2Document)
        pushDocumentsRequest = models.PushDocumentsRequest(optionsHeaders, documentArrayList)

        # Specify the primary key field for the documents.
    	pkField = "id"
        # Send the request with default runtime options.
        response = ha3EngineClient.push_documents(tableName, pkField, pushDocumentsRequest)
        print(response.body)
    except TeaException as e:
        print(f"An API error occurred: {e}")
    except RetryError as e:
        print(f"A connection error occurred: {e}")

if __name__ == "__main__":
    push()

Note:

  • When using the Python SDK, remove the http:// prefix from the endpoint.

  • For access from an on-premises environment or the public internet, use the public endpoint (the one containing public) and ensure that public access is enabled and the whitelist is configured. For access from within the same VPC (for example, from an ECS instance), use the private endpoint.

  • The tableName parameter in the request uses the format <instance_id>_<table_name>, for example, tableName=ha-cn-zpr3dgzxg04_test_vector_api.

A response of {"status": "OK", "code": 200} indicates a successful request.

Step 4: Verify the data

To verify the data, run a query in the Query Test tool by using a primary key or a vector.

In the left navigation bar, click Vector Management > Query Test, and select the Vector Query tab. In the Table Name drop-down list, select the target table, enter a query request in JSON format in the editing area on the left (including parameters such as vector, topK, indexName, and includeVector), and click Search. The query results and time elapsed are displayed on the right.

For more information about query syntax, see Primary key-based query and Vector-based query.

Other SDK references

For SDK examples in other programming languages, see Query data.