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

向量数据库VikingDB

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

概述

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

请求参数

参数名

类型

必须

说明

子字段

类型

必选

说明

task_type

string

是

任务类型,填入data_export

task_params

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>

数据导出时:

  • 如果不填入FilterConds,则无关ExportAll,一定导出全部数据。
  • 如果填入FilterConds:
  • 不写Exportall,或Exportall=false,则默认导出满足条件的数据。
  • 写exportall=true,则强制导出全部数据,此时FilterConds不生效。

export_all

bool

否

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

tos_path

string

是

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

file_type

string

否

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

output_fields

array

否

配置导出字段列表,例如 ["id", "title", "score"];省略时导出全部字段。

示例

请求参数

curl -i -X POST \
  -H 'Content-Type: application/json' \
  -H 'Authorization: HMAC-SHA256 ***' \
  https://api-vikingdb.volces.com/api/task/create \
  -d '{
    "task_type": "data_export",
    "task_params": {
        "collection_name": "example",
        "tos_path": "<YOUR_BUCKET_NAME>/<YOUR_TOS_PATH>",
        "file_type": "parquet",
        "output_fields": ["city", "user_id"],
        "filter_conds": [
            {
                "op": "must",
                "field": "city",
                "conds": ["beijing"]
            },
            {
                "op": "range",
                "field": "user_id",
                "gt": 0,
                "lt": 4
            }
        ]
    }
}'

返回值

属性

说明

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")

参数说明补充

output_fields 类型为 JSON array<string>,例如 ["id", "title", "score"]。省略时导出全部字段;字符串或非字符串元素会返回参数错误。

使用说明修正

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

鉴权说明补充

鉴权说明

示例中的 Authorization: HMAC-SHA256 *** 不是固定 Token。请从火山引擎控制台“密钥管理”获取 AK/SK,并根据 HTTP 方法、URI、查询参数、规范化请求头和请求体摘要生成 HMAC-SHA256 签名。完整步骤参见API 签名调用指南。使用官方 SDK 时由 SDK 自动完成签名,不要在代码或文档中写入真实密钥。

错误处理补充

返回结构补充

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

错误码

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

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