批处理

并行处理数千个输入,支持速率限制和错误处理

概述

批处理会对多个输入 — 来自文件或标准输入 — 运行同一个 API 端点,支持并行执行、速率限制和可配置的错误处理。

基本语法

anysite api <endpoint> --from-file <file> --input-key <key> [OPTIONS]

输入来源

来自文件

# users.txt — one value per line
satyanadella
jeffweiner08
billgates
anysite api /api/linkedin/user --from-file users.txt --input-key user

来自 CSV/JSON 文件

CLI 会根据文件扩展名自动检测格式:

# From CSV (uses the column matching --input-key)
anysite api /api/linkedin/company --from-file companies.csv --input-key company

# From JSONL (uses the field matching --input-key)
anysite api /api/linkedin/user --from-file users.jsonl --input-key user

来自标准输入

cat urls.txt | anysite api /api/linkedin/user --input-key user --stdin

# Chain with other CLI commands
anysite api /api/linkedin/search/users keywords="CTO" count=50 -q --format jsonl | \
  jq -r '.urn.value' | \
  anysite api /api/linkedin/user --input-key user --stdin

并行执行

anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5

更高的并行度会提高吞吐量,但也会增加 API 令牌消耗。建议从 3-5 个并发工作进程开始,并根据您的套餐限制进行调整。

--delay 会在请求之间加入固定的暂停时间(单位:秒),但它仅在 --parallel 保持默认值 1(顺序模式)时生效 — 一旦 --parallel 大于 1,它就不再起作用。

速率限制

anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5 --rate-limit "10/s"

速率限制格式:

错误处理

anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --on-error skip
策略 行为
stop 遇到第一个错误就停止整个批处理(默认)
skip 打印警告,跳过失败的输入,继续处理其余部分
retry 该标志会被接受,但此版本中没有为它实现重试逻辑 — 失败的输入会被静默地从结果中丢弃,批处理不会中止

进度与统计

# Real-time progress bar
anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5 --progress

# Summary statistics after completion (total processed, succeeded, failed, elapsed time)
anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5 --stats

输出选项

所有输出格式选项在批处理中同样适用:

# Save batch results to CSV
anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5 --format csv --output results.csv

# JSONL for streaming
anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5 --format jsonl --output results.jsonl

# Quiet mode for piping
anysite api /api/linkedin/user --from-file users.txt --input-key user \
  --parallel 5 -q --format jsonl | anysite db insert mydb --table profiles --stdin

完整示例

anysite api /api/linkedin/user \
  --from-file linkedin_urls.txt \
  --input-key user \
  --parallel 5 \
  --rate-limit "10/s" \
  --on-error skip \
  --progress \
  --stats \
  --format csv \
  --output enriched_profiles.csv

选项参考

选项 描述
--from-file 输入文件路径(TXT、CSV、JSON、JSONL)
--input-key 每个输入值要映射到的参数名
--stdin 从标准输入而非文件读取输入
--parallel, -j 并发工作进程数量(默认:1)
--delay 请求之间的延迟秒数 — 仅顺序模式有效(默认:0)
--rate-limit 最大请求速率(例如 10/s、100/m、1000/h)
--on-error 错误处理方式:stop、skip、retry(默认:stop)
--progress / --no-progress 显示或隐藏实时进度条
--stats 完成后显示批处理统计信息
--append 追加到现有输出文件而不是覆盖它

后续步骤

Dataset Pipelines

构建具有依赖链和调度功能的多数据源工作流

Database Operations

将批处理结果直接加载到数据库中