按特定条件批量导出Collection中的数据
使用前请先授权 VikingDB 跨服务访问 TOS 去授权
参数名 | 类型 | 必须 | 说明 | 子字段 | 类型 | 必选 | 说明 |
|---|---|---|---|---|---|---|---|
taskType | string | 是 | 任务类型,填入data_export | ||||
taskParams | map | 是 | 任务参数 | collection_name | string | 是 | Collection 名称,必填 |
array | map | 是 | 过滤条件,支持 must 、must_not、range、range_out 算子,使用参考标量过滤 | ||||
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 |
运行示例前,请将其中的 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, "./")
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 为下载得到的对象文件名。
| 字段 | 类型 | 含义 |
|---|---|---|
code | int | 业务状态码;成功时为 0。 |
message | string | 状态说明或错误信息。 |
request_id | string | 请求唯一标识,用于问题排查。 |
data | object / array | 接口业务数据;没有业务数据时可为空。 |
本接口的业务错误码、触发条件和处理建议统一参见向量库 V1 错误码。排查时请保留响应中的 request_id。