This topic describes how to use OpenSearch Vector Search Edition SDKs for Java in asynchronous mode, Java, Python, and Go to query the statistics of a table.
Dependencies
Java
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-ha3engine-vector</artifactId>
<version>1.1.18</version>
</dependency>
Python
# Requires: Python >=3.6
pip install alibabacloud_ha3engine_vector==1.1.18
Go
go get github.com/aliyun/alibabacloud-ha3-go-sdk@v1.1.18-vector
Asynchronous Java
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-ha3engine-async</artifactId>
<version>1.1.8</version>
</dependency>
Parameters
You must configure the following parameters in SDKs for Java and Python: endpoint, instance_id, access_user_name, and access_pass_word.
-
endpoint: the internal or public endpoint.
After you enable public access, you can call the instance from your local environment by using the public endpoint, which contains
publicin its name. You can configure an IP address whitelist to control access. To call the instance from an Elastic Compute Service (ECS) instance, use the API endpoint. This requires both services to use the same vSwitch. You can find the endpoints in the Network Information and API Endpoint sections on the instance details page. -
instance_id: the ID of the OpenSearch Vector Search Edition instance.
In the OpenSearch Vector Search Edition console, go to the instance list page and click the target instance. On the instance details page that appears, find the instance name at the top of the page, which serves as the instance ID, in a format like
ha-cn-xxx. access_user_name: the username.
access_pass_word: the password.
You can find the username and password in the API Endpoint section on the instance details page. This is the password you set when you purchased the instance. It can be changed.
SDK example
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import com.aliyun.ha3engine.vector.Client;
import com.aliyun.ha3engine.vector.models.Config;
import com.aliyun.ha3engine.vector.models.SearchResponse;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;
public class Demo {
/**
* The client for OpenSearch Vector Search Edition.
*/
private Client client;
@Before
public void clientInit() throws Exception {
/*
Initialize the client for OpenSearch Vector Search Edition.
*/
Config config = new Config();
// The instance ID. You can find it on the instance details page. Example: ha-cn-i7*****605.
config.setInstanceId("ha-cn-i7*****605");
// The username. You can find it in the API Endpoint section on the instance details page.
config.setAccessUserName("username");
// The password. You can change it in the API Endpoint section on the instance details page.
config.setAccessPassWord("password");
// The API endpoint. You can find it in the API Endpoint section on the instance details page.
config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
client = new Client(config);
}
@Test
public void stats() throws Exception {
try {
SearchResponse searchResponse = client.stats("test");
System.out.println("Search result:\n" + searchResponse.getBody());
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Python
from alibabacloud_ha3engine_vector.client import Client
from alibabacloud_ha3engine_vector.models import Config
config = Config(
# The API endpoint. You can find it in the API Endpoint section on the instance details page.
endpoint="http://ha-cn-i7*****605.public.ha.aliyuncs.com",
# The username. You can find it in the API Endpoint section on the instance details page.
access_user_name="username",
# The password. You can change it in the API Endpoint section on the instance details page.
access_pass_word="password")
client = Client(config)
result = client.stats("gist")
Go
package main
import (
"fmt"
"github.com/alibabacloud-go/tea/tea"
ha3engine "github.com/aliyun/alibabacloud-ha3-go-sdk/client"
)
func main() {
// Create a client instance for requests.
config := &ha3engine.Config{
// The API endpoint. You can find it in the API Endpoint section on the instance details page.
Endpoint: tea.String("ha-cn-i7*****605.public.ha.aliyuncs.com"),
// The username. You can find it in the API Endpoint section on the instance details page.
AccessUserName: tea.String("username"),
// The password. You can change it in the API Endpoint section on the instance details page.
AccessPassWord: tea.String("password"),
}
// Initialize a client to send requests.
client, _clientErr := ha3engine.NewClient(config)
// If initialization fails, print the error and exit.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
stats(client)
}
/**
* Data statistics
*/
func stats(client *ha3engine.Client) {
response, _requestErr := client.Stats(tea.String("api"))
// If the request fails, print the error and exit.
if _requestErr != nil {
fmt.Println(_requestErr)
return
}
// Print the content of the response.
fmt.Println(response)
}
Java in asynchronous mode
import com.aliyun.ha3engine.async.AsyncClient;
import com.aliyun.ha3engine.async.models.SearchResponse;
import com.aliyun.ha3engine.async.models.StatsRequest;
import com.aliyun.sdk.ha3engine.async.core.AsyncConfigInfoProvider;
import com.aliyun.tea.TeaException;
import darabonba.core.client.ClientOverrideConfiguration;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* @author alibaba
*/
public class SearchDoc {
/**
* The client for OpenSearch Vector Search Edition.
*/
private AsyncClient client;
@Before
public void clientInit() {
// Set the instance username and password. You can find these in the API Endpoint section of the instance details page.
AsyncConfigInfoProvider provider = AsyncConfigInfoProvider.create("username", "password");
// Initialize the asynchronous client.
client = AsyncClient.builder()
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("ha-cn-i7*****605.public.ha.aliyuncs.com")
.setProtocol("http")
).build();
}
@Test
public void searchDoc() {
try {
StatsRequest statsRequest = StatsRequest.builder().tableName("table_name").build();
CompletableFuture<SearchResponse> searchResponseCompletableFuture = client.stats(statsRequest);
String responseBody = searchResponseCompletableFuture.get().getBody();
System.out.println("result: " + responseBody);
} catch (ExecutionException | InterruptedException e) {
System.out.println(e.getMessage());
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Additional information
-
For information about the response, see response parameters in the statistics section of the Traffic API topic.
-
When using the
go get github.com/aliyun/alibabacloud-ha3-go-sdkcommand, you must specify a version tag. The repository contains tags for both OpenSearch Vector Search Edition and OpenSearch Retrieval Engine Edition, so omitting the version tag can result in an incorrect dependency. Ensure you pull the version that corresponds to your product.