You need to enable JavaScript to run this app.
文档中心
向量数据库VikingDB

向量数据库VikingDB

复制全文
下载 pdf
任务创建
Collection 数据过滤导出任务
复制全文
下载 pdf
Collection 数据过滤导出任务

概述

按特定条件批量导出Collection中的数据
使用前请先授权 VikingDB 跨服务访问 TOS 去授权

请求参数

参数名

类型

必须

说明

子字段

类型

必选

说明

taskType

string

是

任务类型,填入data_export

taskParams

map

是

任务参数

collection_name

string

是

Collection 名称,必填

array

map

是

过滤条件,支持 must 、must_not、range、range_out 算子,使用参考标量过滤
过滤导出 city 为 beijing 且 user_id (0, 4) 之间的数据
JSON <br> [ <br> { <br> "op": "must", <br> "field": "city", <br> "conds": ["beijing"] <br> }, <br> { <br> "op": "range", <br> "field": "user_id", <br> "gt": 0, <br> "lt": 4 <br> }, <br> <br> ] <br>

export_all

bool

否

导出全部数据,此时filter不生效。默认为false

tos_path

string

是

将数据文件导入到用户的TOS 路径,格式 :{桶名}/{路径},注意不是域名。必填

file_type

string

否

备份文件类型 json 或者 parquet,默认为 parquet

示例

运行示例前,请完成以下准备:

  • 在 Java 项目中引入 VikingDB SDK,并将示例放入可执行的 main 方法后编译运行。
  • 将 <YOUR_HOST>、<YOUR_REGION> 和 <YOUR_SCHEME> 替换为地域和访问域名中与实例一致的服务地址、地域和协议。
  • 将 <YOUR_AK>、<YOUR_SK> 替换为火山引擎控制台“密钥管理”中创建的 Access Key ID 和 Secret Access Key;请勿把真实凭证提交到代码仓库。
  • 将 Collection、Index、Task 等资源占位符替换为控制台中对应资源的名称或接口返回的 ID。若示例包含 TOS 路径,请从 TOS 控制台确认桶名和对象路径,并使用 tos://<YOUR_BUCKET_NAME>/<YOUR_OBJECT_KEY> 或文档要求的路径格式。

请求参数

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpHost;

import java.util.Map;
import com.volcengine.service.vikingDB.Collection;
import com.volcengine.service.vikingDB.Index;
import com.volcengine.service.vikingDB.VikingDBService;
import com.volcengine.service.vikingDB.common.*;

public class test {
    public static void main(String[] args) throws Exception {
        VikingDBService vikingDBService = new VikingDBService("<YOUR_HOST>", "<YOUR_REGION>", "<YOUR_AK>", "<YOUR_SK>", "<YOUR_SCHEME>");
        HashMap<String, Object> taskParams = new HashMap<>();
        
        List<Map<String, Object>> conditionsList = new ArrayList<>();
        Map<String, Object> condition1 = new HashMap<>();
        condition1.put("op", "must");
        condition1.put("field", "city");
        condition1.put("conds", Arrays.asList("beijing"));
        conditionsList.add(condition1);
        
        Map<String, Object> condition2 = new HashMap<>();
        condition2.put("op", "range");
        condition2.put("field", "user_id");
        condition2.put("gt", 0);
        condition2.put("lt", 4);
        conditionsList.add(condition2);
        
        taskParams.put("filter_conds", conditionsList);
        taskParams.put("collection_name", "example");
        CreateTaskParam taskParam = new CreateTaskParam()
                                    .setTaskType(TaskType.DataExport)
                                    .setTaskParams(taskParams)
                                    .build();
        String task_id = vikingDBService.createTask(taskParam);
    }
}

返回值

属性

说明

task_id

uuid

后续处理

1、从 TOS 下载文件

运行示例前,请将其中的 TOS Bucket、对象路径和本地路径占位符替换为实际值。

import tos
DOMAIN = "api-vikingdb.volces.com"
TOS_ENDPOINT = "tos-cn-beijing.ivolces.com"
REGION = "cn-beijing"
AK = "****"
SK = "****"
COLLECTION_NAME = "example"
BUCKET_NAME = "bucket_name"
TOS_DIR = "tos_dir"

def download(client, bucket_name, object_key, local_path):
    file_path = "{}/{}".format(local_path, object_key)
    try:
        client.get_object_to_file(bucket_name, object_key, file_path)
    except tos.exceptions.TosClientError as e:
        # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
        return 'fail with client error, message:{}, cause: {}'.format(e.message, e.cause)
    except tos.exceptions.TosServerError as e:
        return 'fail with server error : {}'.format(e)
    except Exception as e:
        return 'fail with unknown error: {}'.format(e)
    return ''

client = tos.TosClientV2(AK, SK, TOS_ENDPOINT, REGION)
download(client, BUCKET_NAME, TOS_DIR, "./")

2、解析 parquet 类型数据

import pyarrow.parquet as pq
def process_parquet(file_path):
    parquet_file = pq.ParquetFile(file_path)

    file_data_count = sum(parquet_file.metadata.row_group(i).num_rows for i in range(parquet_file.num_row_groups))
    schema = parquet_file.schema.to_arrow_schema()
    row_iterator = parquet_file.iter_batches(batch_size=100)
    # 迭代读取数据
    for batch in row_iterator:
        df = batch.to_pandas()
        for index, row in df.iterrows():
            """
            do what you want
            """
            print(row)
    return ''

process_parquet("./tos_dir/1.parquet")

使用说明修正

步骤 1 将对象下载到 ./tos_dir,步骤 2 使用 process_parquet("./tos_dir/1.parquet") 读取同一文件;1.parquet 为下载得到的对象文件名。

错误处理补充

返回结构补充

字段类型含义
codeint业务状态码;成功时为 0。
messagestring状态说明或错误信息。
request_idstring请求唯一标识,用于问题排查。
dataobject / array接口业务数据;没有业务数据时可为空。

错误码

本接口的业务错误码、触发条件和处理建议统一参见向量库 V1 错误码。排查时请保留响应中的 request_id。

最近更新时间:2026.09.08 15:30:59
这个页面对您有帮助吗?
有用
有用
无用
无用