You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Vector
GetVectors(TOS Vectors Java SDK)
Copy page
Download PDF
GetVectors(TOS Vectors Java SDK)
The getVectors interface retrieves vector data from a specified vector collection. This article explains how to use the getVectors interface of the TOS Vectors Java SDK to query and return vector IDs along with their corresponding vector values.​
Notes​
  • To prevent key leakage, the sample code in this article reads the Access Key ID and Secret Access Key from environment variables. Therefore, before using the SDK, you need to configure long-term access credentials. For details, see Configure Long-Term Access Credentials.​
  • The TOS Vectors Java SDK supports using temporary access credentials; it also supports configuring timeout mechanisms, retry strategies, and other mechanisms. You can flexibly set these according to business needs to enhance application security. For details, see Common Configuration Scenarios.​
Permissions required​
The main account has all permissions by default, but IAM users have no permissions by default. If you need child accounts to perform getVectors operations, the main account must grant the following permissions to the child account through IAM policies and bucket policies:​
tosvectors:GetVectors​
Code example​
Batch query vectors by vector key​
​
// Batch query vectors based on vector keys.​
import com.volcengine.tos.TOSVectorsClientBuilder;​
import com.volcengine.tos.TOSVectors;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.vectors.GetVectorsInput;​
import com.volcengine.tos.model.vectors.GetVectorsOutput;​
import com.volcengine.tos.model.vectors.Vector;​
​
import java.util.Arrays;​
import java.util.List;​
​
public class GetVectorsExample {​
public static void main(String[] args) {​
String endpoint = 'Provide your endpoint';​
String region = 'Provide your region';​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
String accountID = System.getenv("TOS_ACCOUNT_ID");​
​
// Fill in the vector storage bucket name and index name.​
String vectorBucketName = 'Provide your vector bucket name';​
String indexName = "testindex";​
​
// Initialize client.​
TOSVectors client = new TOSVectorsClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try {​
// Set the list of vector keys to query.​
List<String> vectorKeys = Arrays.asList("product-vector-001", "product-vector-002", "product-vector-003");​
​
// Construct a batch query vector request.​
GetVectorsInput input = GetVectorsInput.builder()​
.vectorBucketName(vectorBucketName)​
.accountId(accountID)​
.indexName(indexName)​
.keys(vectorKeys)​
.returnData(true) // Return vector data.​
.returnMetadata(true) // Return metadata information.​
.build();​
​
// Perform batch query vector operations.​
GetVectorsOutput output = client.getVectors(input);​
​
// Process the return result.​
System.out.println("批量查询向量成功");​
System.out.println("RequestID: " + output.getRequestInfo().getRequestId());​
System.out.println("StatusCode: " + output.getRequestInfo().getStatusCode());​
​
// Print the queried vector information.​
if (output.getVectors() != null && !output.getVectors().isEmpty()) {​
System.out.println("Successfully queried " + output.getVectors().size() + " vectors:");​
for (int i = 0; i < output.getVectors().size(); i++) {​
Vector vector = output.getVectors().get(i);​
System.out.println("Vector " + (i + 1) + ":");​
System.out.println(" Key: " + vector.getKey());​
​
if (vector.getData() != null && vector.getData().getFloat32() != null) {​
System.out.println(" Dimension: " + vector.getData().getFloat32().length);​
System.out.println(" Data Preview: [" + vector.getData().getFloat32()[0] + ", " + ​
vector.getData().getFloat32()[1] + ", " + vector.getData().getFloat32()[2] + "...]");​
}​
​
if (vector.getMetadata() != null) {​
System.out.println(" Metadata: " + vector.getMetadata());​
}​
}​
} else {​
System.out.println("The specified vector was not found");​
}​
} catch (TosClientException e) {​
// Operation failed, caught Client exception. Generally, it is due to incorrect request parameters, and in this case, the request was not sent.​
System.out.println("Operation failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
// Operation failed, server exception captured, and detailed error information returned from the server can be obtained.​
System.out.println("Operation failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
// As a fallback to catch other exceptions, it generally won't execute here.​
System.out.println("Operation failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Related documentation​
For the API documentation of GetVectors, see GetVectors.​
Last updated: 2026.02.06 18:01:47