You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中无过滤打印Milvus数据库中存储的所有数据?

在Java中无过滤获取并打印Milvus全量数据

Milvus Java SDK本身支持无过滤条件的全量数据查询,不需要构建诸如id >= 0这类取巧过滤条件,核心是利用QueryParam或SearchParam不设置expr参数的特性,再配合分页逻辑获取所有数据(避免一次性返回过大数据集导致内存溢出)。

实现步骤与代码示例

1. 初始化Milvus客户端

确保已引入Milvus Java SDK依赖,先建立客户端连接:

import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;

MilvusServiceClient client = new MilvusServiceClient(
    ConnectParam.newBuilder()
        .withUri("http://localhost:19530")
        .build()
);

2. 无过滤全量查询(标量字段优先用Query)

如果只需要获取标量字段或主键,使用QueryParam更高效,不设置expr即为无过滤:

import io.milvus.param.R;
import io.milvus.param.query.QueryParam;
import io.milvus.response.QueryResultsWrapper;
import java.util.List;

// 分页参数设置
long offset = 0;
final long pageSize = 1000; // 根据内存情况调整每页大小
String collectionName = "your_collection_name";
List<String> outputFields = List.of("id", "your_scalar_field1", "your_scalar_field2"); // 指定要返回的字段

while (true) {
    R<QueryResultsWrapper> response = client.query(
        QueryParam.newBuilder()
            .withCollectionName(collectionName)
            .withOutFields(outputFields)
            .withOffset(offset)
            .withLimit(pageSize)
            // 不设置withExpr(),即为无过滤全量查询
            .build()
    );

    if (response.getStatus() != R.Status.Success.getCode()) {
        System.err.println("查询失败: " + response.getMessage());
        break;
    }

    QueryResultsWrapper wrapper = response.getData();
    List<QueryResultsWrapper.RowRecord> rows = wrapper.getRowRecords();
    if (rows.isEmpty()) {
        break; // 没有更多数据,退出循环
    }

    // 打印当前页数据
    for (QueryResultsWrapper.RowRecord row : rows) {
        System.out.println(row);
    }

    offset += pageSize;
}

3. 若需向量字段,使用Search(无过滤)

如果需要获取向量字段,使用SearchParam,同样不设置expr,并指定空的向量(或任意向量,无过滤时相似度匹配不生效):

import io.milvus.param.search.SearchParam;
import io.milvus.response.SearchResultsWrapper;
import java.util.Arrays;

long offset = 0;
final long pageSize = 1000;
String collectionName = "your_collection_name";
List<String> outputFields = List.of("id", "your_vector_field");

while (true) {
    R<SearchResultsWrapper> response = client.search(
        SearchParam.newBuilder()
            .withCollectionName(collectionName)
            .withOutFields(outputFields)
            .withOffset(offset)
            .withLimit(pageSize)
            .withVectors(Arrays.asList(new float[128])) // 向量维度需与集合一致
            .withVectorFieldName("your_vector_field")
            .withMetricType(io.milvus.common.clientenum.MetricType.L2)
            // 不设置withExpr(),无过滤
            .build()
    );

    if (response.getStatus() != R.Status.Success.getCode()) {
        System.err.println("搜索失败: " + response.getMessage());
        break;
    }

    SearchResultsWrapper wrapper = response.getData();
    List<SearchResultsWrapper.IDScore> results = wrapper.getIDScoreList();
    if (results.isEmpty()) {
        break;
    }

    // 打印结果(包含向量和主键)
    for (SearchResultsWrapper.IDScore result : results) {
        System.out.println("ID: " + result.getLongID() + ", 向量: " + Arrays.toString(result.getVector()));
    }

    offset += pageSize;
}

关键说明

  • 不设置expr参数是Milvus SDK原生支持的无过滤查询方式,并非取巧手段,官方SDK设计中默认无expr即匹配所有数据。
  • 必须使用分页:Milvus限制单次查询/搜索的返回量,避免内存过载,因此需要循环分页直到获取所有数据。
  • 选择Query还是Search:仅需标量/主键用Query,需要向量字段用Search,前者性能更优。

内容的提问来源于stack exchange,提问作者Arohi Srivastav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 18:10:08