# 批量处理 CSV

`tomba bulk` 读取 CSV 文件，从您的机器为每一行发送 1 个 API 请求，并将结果写入新的 CSV 文件。它不使用 [Bulk API](/bulks)，因此每一行都按单独的请求计费，并计入速率限制。

## 与 Bulk API 的区别

|               | `tomba bulk`                                                                                                                                                             | [Bulk API](/bulks)                                                       |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------ |
| 运行位置 | 在您的机器上：每行 1 个请求                                                                                                                                     | 在 Tomba 的服务器上：整个列表 1 个任务                           |
| 积分       | 每一行按单个请求计费（[积分消耗](/usage-and-quotas#credit-costs)）；[重复请求规则](/usage-and-quotas#duplicate-requests)适用于每一行 | 按任务计费，在您首次下载结果时扣除（[计费](/bulks#billing)） |
| 速率限制   | 每一行都计入您的[速率限制](/rate-limits)                                                                                                                  | [限制](/bulks#limits)中的任务限制                                |
| 结果       | 随各行完成而写入的 CSV 文件                                                                                                                                      | 任务完成后供您下载的 CSV 文件                           |

## 用法

```text
tomba bulk --file <input.csv> --type <type> [column flags] [--output <output.csv>]
```

输入必须是带标题行的逗号分隔文件。比标题行短的行会被补齐，输入单元格为空的行会被跳过，不发送请求。

## 类型

| `--type`           | 调用                                                    | 输入列                                |
| ------------------ | -------------------------------------------------------- | -------------------------------------------- |
| `enrich`（默认） | [`GET /v1/enrich`](/api/finder#email-enrichment)         | 邮箱                                        |
| `verify`           | [`GET /v1/email-verifier`](/api/verifier#email-verifier) | 邮箱                                        |
| `finder`           | [`GET /v1/email-finder`](/api/finder#email-finder)       | 域名，以及名字和姓氏或全名 |
| `search`           | [`GET /v1/domain-search`](/api/finder#domain-search)     | 域名                                       |
| `author`           | [`GET /v1/author-finder`](/api/finder#author-finder)     | 文章 URL                                  |
| `linkedin`         | [`GET /v1/linkedin`](/api/finder#linkedin-finder)        | LinkedIn 个人资料 URL                         |
| `phone`            | [`GET /v1/phone-finder`](/api/phone#phone-finder)        | 邮箱、域名或 LinkedIn 个人资料 URL       |
| `phone-validator`  | [`GET /v1/phone-validator`](/api/phone#phone-validator)  | 电话号码，以及可选的国家代码  |
| `company`          | [`GET /v1/companies/find`](/api/enrichment#company-api)  | 域名                                       |
| `similar`          | [`GET /v1/similar`](/api/domain#similar)                 | 域名                                       |
| `sources`          | [`GET /v1/email-sources`](/api/email#email-sources)      | 邮箱                                        |

## 标志

| 标志                 | 类型   | 默认值                | 说明                                                                                                                       |
| -------------------- | ------ | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `--file`             | string | —                      | 输入 CSV 文件。必填。                                                                                                         |
| `--type`             | string | `enrich`               | 对每一行运行的查询。请参阅[类型](#types)。                                                                                  |
| `--column`           | string | —                      | 邮箱列。对于 `phone-validator`，在找不到电话列时也用作电话列。                                 |
| `--domain-col`       | string | —                      | 域名列。                                                                                                                    |
| `--first-col`        | string | —                      | 名字列，用于 `finder`。                                                                                                  |
| `--last-col`         | string | —                      | 姓氏列，用于 `finder`。                                                                                                   |
| `--full-name-col`    | string | —                      | 全名列，用于 `finder`，代替名字和姓氏。                                                                   |
| `--url-col`          | string | —                      | URL 列，用于 `author`、`linkedin`，以及与 `phone` 一起使用的 LinkedIn URL。                                                             |
| `--phone-col`        | string | —                      | 电话列，用于 `phone-validator`。                                                                                              |
| `--country-code-col` | string | —                      | 国家代码列，用于 `phone-validator`。                                                                                       |
| `--enrich-mobile`    | bool   | `false`                | 同时返回电话号码，用于 `enrich`、`finder` 和 `linkedin`。电话数据会改变[积分消耗](/usage-and-quotas#credit-costs)。 |
| `--full`             | bool   | `false`                | 每行最多返回 10 个电话号码，用于 `phone`。                                                                               |
| `--concurrency`      | int    | `0`                    | 并行工作线程数。`0` 表示根据您的套餐选择数量。最大值为 60。                                             |
| `--no-resume`        | bool   | `false`                | 覆盖输出文件并处理每一行。                                                                                  |
| `--output`, `-o`     | string | `<input>_enriched.csv` | 输出文件。                                                                                                                      |

[全局标志](/cli/commands#global-flags) `--key`、`--secret` 和 `--no-color` 同样适用。

## 映射列

列名匹配时不区分大小写。如果您没有传入列标志，CLI 会查找以下标题名称：

| 输入        | 标题名称                                                            |
| ------------ | ----------------------------------------------------------------------- |
| 邮箱        | `email`, `e-mail`, `email_address`, `emailaddress`, `mail`              |
| 域名       | `domain`, `company_domain`, `website`, `company_website`                |
| 名字   | `first_name`, `firstname`, `first`, `fname`                             |
| 姓氏    | `last_name`, `lastname`, `last`, `lname`                                |
| 全名    | `full_name`, `fullname`, `full name`                                    |
| URL          | `url`, `link`, `linkedin`, `linkedin_url`, `article_url`, `profile_url` |
| 电话        | `phone`, `phone_number`, `phonenumber`, `tel`, `telephone`              |
| 国家代码 | `country_code`, `countrycode`, `country`                                |

`--column` 设置的是邮箱列，而不是域名列。对于需要域名的类型，请传入 `--domain-col`：如果使用 `--column domain`，CLI 会将该列视为邮箱地址，并要求您指定域名列。

找不到必需的列时，CLI 会让您从列表中选择一列。如果没有交互式终端（例如在 CI 中），CLI 会以 `Could not find ... column` 退出；在这种情况下，请显式传入列标志。

## 输出文件

输出文件先包含您的输入列，然后是该类型的结果列。`search` 和 `similar` 会为找到的每个邮箱地址或公司各写入 1 行，并重复输入列；`search` 获取第一页结果，使用 API 的默认 `limit`。结果会在工作线程返回时写入，因此输出行的顺序可能与输入不同。

运行结束时，CLI 会输出摘要，列出找到、未找到和跳过的行数，以及因客户端错误（4xx 和网络错误）或服务器错误（5xx）而失败的行数。

## 恢复运行

默认情况下，如果输出文件已存在且包含预期的列，CLI 会跳过其中已有的行，并追加其余行。如果输出文件中有一行的输入值与某行相同，该行即视为已完成。

失败的行（例如因 `429` 或 `402` 失败）会以空结果列写入，因此恢复运行时会将其视为已完成，不会重试。要重试这些行，请从输出文件中删除它们，然后再次运行命令，或使用 `--no-resume` 重新运行全部内容。CLI 在收到 `429` 后不会重试或降速；如果摘要显示有客户端错误，请降低 `--concurrency`，并参阅[处理 429 响应](/rate-limits#handle-429-responses)。

<Callout type="caution">
    如果输出文件已存在但列不匹配（例如您更改了 `--type`），CLI 会开始全新运行并覆盖该文件。默认输出文件名不包含类型，因此请传入 `--output`，将每种类型的结果保存在单独的文件中。
</Callout>

## 示例

```bash
# Verify the addresses in the "Email Address" column
tomba bulk --file leads.csv --type verify --column "Email Address"

# Find addresses from domain and name columns
tomba bulk --file prospects.csv --type finder --domain-col company_domain --first-col first --last-col last

# Search each domain and write the results to a chosen file
tomba bulk --file domains.csv --type search --domain-col domain --output domain-search.csv

# Validate phone numbers, using a country-code column for local numbers
tomba bulk --file phones.csv --type phone-validator --phone-col phone --country-code-col country
```
