如何通过Java SDK获取Doc

本文介绍如何通过Java SDK,根据id或id列表获取Collection中已存在的Doc。


说明

如果指定id不存在,则该id对应的Doc为空。

前提条件

接口定义

Java示例:

swift 复制代码
// class DashVectorCollection

// 同步接口
public Response<Map<String, Doc>> fetch(FetchDocRequest fetchDocRequest);

// 异步接口
public ListenableFuture<Response<Map<String, Doc>>> fetchAsync(FetchDocRequest fetchDocRequest);

使用示例

说明

  1. 需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。
  2. 本示例需要参考新建Collection-使用示例提前创建好名称为quickstart的Collection,并参考插入Doc提前插入部分数据。

Java示例:

java 复制代码
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.DashVectorCollection;
import com.aliyun.dashvector.common.DashVectorException;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.FetchDocRequest;
import com.aliyun.dashvector.models.responses.Response;

import java.util.Map;

public class Main {
    public static void main(String[] args) throws DashVectorException {
        DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");
        DashVectorCollection collection = client.get("quickstart");

        // 构建 FetchDocRequest
        FetchDocRequest request = FetchDocRequest.builder()
            .id("1")
            .build();

        // 发送获取Doc请求
        Response<Map<String, Doc>> response = collection.fetch(request);

        System.out.println(response);
        // example output:
        // {
        //     "code":0,
        //     "message":"Success",
        //     "requestId":"489c5cda-3ffc-4171-b6e0-1837b932962b",
        //     "output":{
        //         "1":{
        //             "id":"1",
        //             "vector":{"value":[0.1,0.2,0.3,0.4]},
        //             "fields":{
        //                 "name":"zhangsan",
        //                 "age":20,
        //                 "weight":100.0,
        //                 "anykey1":"String",
        //                 "anykey2":1,
        //                 "anykey3":true,
        //                 "anykey4":3.1415926
        //             },
        //             "score":0
        //         }
        //     }
        // }
    }
}

入参描述

通过FetchDocRequestBuilder构造FetchDocRequest对象,其可用方法如下表所示:

方法 必填 默认值 描述
ids(List ids) - 文档主键列表
id(String id) -
partition(String partition) default 分区名称
build() - - 构造FetchDocRequest对象
相关推荐
呆呆小金人10 小时前
SQL字段对齐:性能优化与数据准确的关键
大数据·数据仓库·sql·数据库开发·etl·etl工程师
Yeats_Liao10 小时前
时序数据库系列(三):InfluxDB数据写入Line Protocol详解
数据库·后端·时序数据库
王元_SmallA10 小时前
Redis Desktop Manager(Redis可视化工具)安装
java·后端
好好研究11 小时前
Spring框架 - 开发方式
java·后端·spring
hashiqimiya12 小时前
springboot后端的接口headers
java·spring boot·后端
ss27312 小时前
Springboot + vue 医院管理系统
vue.js·spring boot·后端
间彧12 小时前
InfluxDB详解与应用实战
后端
间彧12 小时前
对比InfluxDB与Prometheus在监控场景下的特点及选型建议
后端
zskj_zhyl13 小时前
智慧康养新篇章:七彩喜如何重塑老年生活的温度与尊严
大数据·人工智能·科技·物联网·生活