You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
向量
PutVectors(TOS Vectors Java SDK)
复制全文
下载 pdf
PutVectors(TOS Vectors Java SDK)
PutVectors 接口用于写入向量数据,本文介绍如何通过 TOS Vectors Java SDK 的 PutVectors 接口将向量集批量写入指定存储空间。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见 配置长期访问凭证。​
  • TOS Vectors Java SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见 常见配置场景。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用 IAM 用户执行 PutVectors 操作,主账号需通过 IAM策略和桶策略为该 IAM 用户授予以下权限:tosvectors:PutVectors​
代码示例​
向量索引中批量插入向量​
​
// 向量索引中批量插入向量​
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.PutVectorsInput;​
import com.volcengine.tos.model.vectors.PutVectorsOutput;​
import com.volcengine.tos.model.vectors.Vector;​
import com.volcengine.tos.model.vectors.VectorData;​
​
import java.util.ArrayList;​
import java.util.HashMap;​
import java.util.List;​
import java.util.Map;​
​
public class PutVectorsExample {​
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");​
​
// 填写向量存储桶名称和索引名称​
String vectorBucketName = "Provide your vector bucket name";​
String indexName = "testindex";​
​
// 初始化客户端​
TOSVectors client = new TOSVectorsClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try {​
// 创建要插入的向量列表​
List<Vector> vectors = new ArrayList<>();​
​
// 创建第一个向量​
float[] vectorData1 = new float[128];​
for (int i = 0; i < 128; i++) {​
vectorData1[i] = (float) Math.random();​
}​
Map<String, Object> metadata1 = new HashMap<>();​
metadata1.put("category", "electronics");​
metadata1.put("timestamp", System.currentTimeMillis());​
metadata1.put("source", "user-upload");​
​
Vector vector1 = Vector.builder()​
.key("product-vector-001")​
.data(VectorData.builder().float32(vectorData1).build())​
.metadata(metadata1)​
.build();​
vectors.add(vector1);​
​
// 创建第二个向量​
float[] vectorData2 = new float[128];​
for (int i = 0; i < 128; i++) {​
vectorData2[i] = (float) Math.random();​
}​
Map<String, Object> metadata2 = new HashMap<>();​
metadata2.put("category", "clothing");​
metadata2.put("timestamp", System.currentTimeMillis());​
metadata2.put("source", "batch-import");​
​
Vector vector2 = Vector.builder()​
.key("product-vector-002")​
.data(VectorData.builder().float32(vectorData2).build())​
.metadata(metadata2)​
.build();​
vectors.add(vector2);​
​
// 创建第三个向量​
float[] vectorData3 = new float[128];​
for (int i = 0; i < 128; i++) {​
vectorData3[i] = (float) Math.random();​
}​
Map<String, Object> metadata3 = new HashMap<>();​
metadata3.put("category", "books");​
metadata3.put("timestamp", System.currentTimeMillis());​
metadata3.put("source", "manual-entry");​
​
Vector vector3 = Vector.builder()​
.key("product-vector-003")​
.data(VectorData.builder().float32(vectorData3).build())​
.metadata(metadata3)​
.build();​
vectors.add(vector3);​
​
// 构建批量插入向量请求​
PutVectorsInput input = PutVectorsInput.builder()​
.vectorBucketName(vectorBucketName)​
.accountId(accountID)​
.indexName(indexName)​
.vectors(vectors)​
.build();​
​
// 执行批量插入向量操作​
PutVectorsOutput output = client.putVectors(input);​
​
// 处理返回结果​
System.out.println("批量插入向量成功");​
System.out.println("RequestID: " + output.getRequestInfo().getRequestId());​
System.out.println("StatusCode: " + output.getRequestInfo().getStatusCode());​
System.out.println("成功插入向量数量: " + vectors.size());​
​
// 打印插入的向量信息​
for (int i = 0; i < vectors.size(); i++) {​
Vector vector = vectors.get(i);​
System.out.println("向量 " + (i + 1) + ":");​
System.out.println(" Key: " + vector.getKey());​
System.out.println(" Dimension: " + vector.getData().getFloat32().length);​
System.out.println(" Metadata: " + vector.getMetadata());​
}​
​
} catch (TosClientException e) {​
// 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送​
System.out.println("操作失败");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
// 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息​
System.out.println("操作失败");​
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) {​
// 作为兜底捕获其他异常,一般不会执行到这里​
System.out.println("操作失败");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
​
最近更新时间:2026.08.10 17:31:01
这个页面对您有帮助吗?
有用
有用
无用
无用