Create a DashVector client
To interact with DashVector collections, vectors, and similarity searches through your application, you first need a DashVectorClient instance that connects to the DashVector server. This page shows how to create one using the Java SDK.
Quick start
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.common.DashVectorException;
public class Main {
public static void main(String[] args) throws DashVectorException {
DashVectorClient client = new DashVectorClient(
"<your-api-key>",
"<your-cluster-endpoint>"
);
// Use the client to manage collections, insert vectors, and run searches.
}
}Replace <your-api-key> and <your-cluster-endpoint> with your actual values. For details, see Initialize the client.
Prerequisites
Before you begin, make sure that you have:
A DashVector cluster. For details, see Create a cluster
An API key. For details, see Manage API keys
The latest DashVector Java SDK. For details, see Install DashVector SDK
Initialize the client
The DashVectorClient class provides two constructors:
package com.aliyun.dashvector;
// Initialize with an API key and cluster endpoint
DashVectorClient(String apiKey, String endpoint);
// Initialize with a configuration object
DashVectorClient(DashVectorClientConfig config);Replace the following placeholders with your actual values:
| Placeholder | Description | Where to find it |
|---|---|---|
<your-api-key> | API key for authentication | Manage API keys |
<your-cluster-endpoint> | Cluster endpoint URL | Cluster details page in the console |
Do not hardcode API keys in production code. Store them as environment variables or use a secrets manager.
Basic initialization
Pass the API key and endpoint directly:
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.common.DashVectorException;
public class Main {
public static void main(String[] args) throws DashVectorException {
DashVectorClient client = new DashVectorClient(
"<your-api-key>",
"<your-cluster-endpoint>"
);
}
}Configuration-based initialization
Use DashVectorClientConfig to set additional options such as a custom timeout:
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.DashVectorClientConfig;
import com.aliyun.dashvector.common.DashVectorException;
public class Main {
public static void main(String[] args) throws DashVectorException {
DashVectorClientConfig config = DashVectorClientConfig.builder()
.apiKey("<your-api-key>")
.endpoint("<your-cluster-endpoint>")
.timeout(10f) // Timeout in seconds. Set to -1 for no timeout.
.build();
DashVectorClient client = new DashVectorClient(config);
}
}Configuration parameters
Use the DashVectorClientConfigBuilder methods to build a DashVectorClientConfig object.
| Method | Required | Default | Description |
|---|---|---|---|
apiKey(String apiKey) | Yes | - | API key for authentication |
endpoint(String endpoint) | Yes | - | Cluster endpoint. Available on the cluster details page in the console |
timeout(Float timeout) | No | 10.0f | Timeout period in seconds. Set to -1 for no timeout |
build() | - | - | Builds the DashVectorClientConfig object |
If DashVectorException is thrown during client initialization, you can analyze the cause based on the exception details.
What's next
After you create a client, use it to perform operations on collections such as creating a collection, inserting vectors, and running similarity searches. For details, see the DashVector API documentation.