使用 Qoder 将 CSV 文件导入 Elasticsearch

0 阅读4分钟

前言

在日常数据处理工作中,将本地 CSV 数据导入 Elasticsearch 是一个常见需求。传统方式需要手动编写脚本、处理字段映射、配置连接参数等繁琐步骤。本文将演示如何通过 Qoder 智能助手,仅用几句对话,就完成从 CSV 文件到 Elasticsearch 索引的全流程。

场景说明

我们有一份 IMDB 电影数据文件 imdb_movies.csv,包含电影名称、评分、类型、概述、演职员、票房等字段,共约 10,000 条记录。目标是将其导入到 Elasticsearch 集群中,索引名为 test。

注:有关 "imdb_movies.csv" 的下载,请参考文章 ”如何写入 IMDB 电影数据并针对它运用 AI Agent Builder 对它进行分享“

数据样例

`

1.  names,date_x,score,genre,overview,crew,orig_title,status,orig_lang,budget_x,revenue,country
2.  Creed III,03/02/2023,73.0,"Drama, Action","After dominating the boxing world...",Michael B. Jordan,...,Creed III,Released,English,75000000.0,271616668.0,AU
3.  Avatar: The Way of Water,12/15/2022,78.0,"Science Fiction, Adventure, Action","Set more than a decade after...",Sam Worthington,...,Avatar: The Way of Water,Released,English,460000000.0,2316794914.0,AU

`AI代码解释

操作步骤

第一步:告诉 Qoder 你的需求

只需一句话描述任务:

"I have an imdb at /Users/liuxg/python/imdb/imdb_movies.csv, please index it to Elasticsearch, and the index name is 'test'"

Qoder 会自动:

  1. 读取 CSV 文件,分析字段结构
  2. 加载内置的 elasticsearch-file-ingest 技能
  3. 安装所需依赖

第二步:提供连接信息

Qoder 会询问 Elasticsearch 的连接方式。支持以下几种:

方式说明
localhost:9200本地默认实例,使用 elastic/changeme
Elastic Cloud云端部署,提供 URL + API Key
自定义连接指定 URL、用户名/密码或 API Key

本例中,我们选择自定义连接,提供:

  • URL: https://localhost:9200
  • API Key: b0w1VWU2RUJDNDM5TkpDMDVfOXA6TTBzUkxNN0hKc3ZFMzNRMExQdzU1QQ

第三步:自动推断映射并导入

Qoder 使用 --infer-mappings 参数,让 Elasticsearch 自动分析 CSV 数据结构 并生成合适的字段映射:

`

1.  node scripts/ingest.js \
2.    --file /Users/liuxg/python/imdb/imdb_movies.csv \
3.    --infer-mappings \
4.    --target test \
5.    --node https://localhost:9200 \
6.    --api-key "your-api-key" \
7.    --delete-index

`AI代码解释

注: 在写入文档之前,我们也可以告诉 Qoder 你所需要的 mapping 是什么样子的。这个 mapping 也可以和自动识别的 mapping 有所不同。mapping 的格式可以是这样的。你可以根据自己的需要进行调整。

`

1.  {
2.    "test": {
3.      "mappings": {
4.        "properties": {
5.          "budget_x": {
6.            "type": "double"
7.          },
8.          "country": {
9.            "type": "keyword"
10.          },
11.          "crew": {
12.            "type": "text"
13.          },
14.          "date_x": {
15.            "type": "keyword"
16.          },
17.          "genre": {
18.            "type": "keyword"
19.          },
20.          "names": {
21.            "type": "text"
22.          },
23.          "orig_lang": {
24.            "type": "keyword"
25.          },
26.          "orig_title": {
27.            "type": "text"
28.          },
29.          "overview": {
30.            "type": "text"
31.          },
32.          "revenue": {
33.            "type": "double"
34.          },
35.          "score": {
36.            "type": "double"
37.          },
38.          "status": {
39.            "type": "keyword"
40.          }
41.        }
42.      }
43.    }
44.  }

`AI代码解释![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

第四步:验证结果

导入完成后,Qoder 自动验证数据:

`

1.  // 文档计数
2.  {"count": 10178, "_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}}

4.  // 自动推断的字段映射
5.  {
6.    "test": {
7.      "mappings": {
8.        "properties": {
9.          "names":      {"type": "text"},
10.          "date_x":     {"type": "keyword"},
11.          "score":      {"type": "double"},
12.          "genre":      {"type": "keyword"},
13.          "overview":   {"type": "text"},
14.          "crew":       {"type": "text"},
15.          "orig_title": {"type": "text"},
16.          "status":     {"type": "keyword"},
17.          "orig_lang":  {"type": "keyword"},
18.          "budget_x":   {"type": "double"},
19.          "revenue":    {"type": "double"},
20.          "country":    {"type": "keyword"}
21.        }
22.      }
23.    }
24.  }

`AI代码解释![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

字段类型推断逻辑

Qoder 调用 Elasticsearch 的 _text_structure/find_structure  API 自动推断字段类型:

  • text — 适用于需要全文检索的字段(如 names、overview、crew)
  • keyword — 适用于精确匹配和聚合的字段(如 country、status、genre)
  • double — 适用于数值型字段(如 score、budget_x、revenue)

进阶用法

自定义字段映射

如果需要精确控制字段类型,可以手动提供映射文件:

`

1.  node scripts/ingest.js \
2.    --file data.csv \
3.    --source-format csv \
4.    --mappings mappings.json \
5.    --target my-index

`AI代码解释

数据转换

在导入过程中对数据进行清洗或转换,创建 transform.js:

`

1.  export default function transform(doc) {
2.    return {
3.      ...doc,
4.      // 将日期字符串转为标准格式
5.      date_x: new Date(doc.date_x).toISOString(),
6.      // 添加计算字段
7.      profit: (doc.revenue || 0) - (doc.budget_x || 0),
8.    };
9.  }

`AI代码解释

然后执行:

`

1.  node scripts/ingest.js \
2.    --file data.csv \
3.    --source-format csv \
4.    --transform transform.js \
5.    --target my-index

`AI代码解释

跨 集群 迁移

从 ES 8.x 迁移到 ES 9.x:

`

1.  node scripts/ingest.js \
2.    --source-index old-logs \
3.    --target new-logs \
4.    --node https://es8.example.com:9200 --api-key es8-key \
5.    --target-node https://es9.example.com:9200 --target-api-key es9-key

`AI代码解释

Qoder 的聊天记录

我们可以在 Kibana 中进行查看:

从上面,我们可以看到有 10000 个稳定被成功地写入。

总结

项目结果
源文件/Users/liuxg/python/imdb/imdb_movies.csv
 目标索引test
文档数量10,178 条
ES 版本9.5.0
耗时约 30 秒(含依赖安装)
交互轮次4 轮对话

通过 Qoder,无需编写任何代码,仅通过自然语言对话即可完成 CSV 到 Elasticsearch 的数据导入。Qoder 自动处理了依赖安装、连接配置、字段映射推断、数据验证等全部环节,极大简化了数据导入流程。