elasticsearch 数据导入/导出工具 elasticdump 的使用

背景

elasticdump
需要将 elasticsearch 数据导出至 JSON(或 csv)

步骤

前置依赖:需要 Node.js 环境(npm)

# 安装
$ npm install -g elasticdump

查看帮助

# 详情见:https://github.com/elasticsearch-dump/elasticsearch-dump
$ elasticdump --help

脚本示例

# 编写导出脚本
$ vim run.sh
#!/bin/sh

# 查询语句
searchbody=`cat << EOF
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "goods.category": 20
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": 1679414400000,
              "lt": 1679500800000,
              "format":"epoch_millis"
            }
          }
        }
      ]
    }
  }
}
EOF`

index=goods-2023.03.23
endpoint="http://username:password@127.0.0.1:9200/goods-2023.03.23"
outputpath=es-data/2023-03-23.json

# --input 指定来源
# --output 导出的路径(如果以 csv 结尾,则会自动保存为 csv 格式)
# --quiet 不输出任何日志信息
# --scrollTime 用来指定每次滚动查询的时间间隔(当我们需要从 elasticsearch 中检索大量数据时,通常需要使用滚动查询来避免一次性检索过多数据导致内存溢出或性能下降的问题)
# --limit 指定每次滚动查询的文档数量
# --noRefresh 禁用索引的自动刷新功能(当需要执行大量索引操作时,如果每次操作都会自动刷新索引,将导致性能下降)
# --maxRows 指定每次滚动查询的最大文档数量
# --concurrency 并发执行的数量
# --transform 对结果进行转换(如提取特定字段、转换数据格式等)
# --searchBody 查询语句
elasticdump --input="$endpoint/$index" \
  --output=$outputpath \
  --quiet --scrollTime=30m --limit=10000 --noRefresh --maxRows=1000000 \
  --concurrency=2 \
  --transform @transform.js \
  --searchBody "$searchbody"

# 结果转换脚本
$ vim transform.js
module.exports = function (doc, options = {}) {
  var item = doc._source.goods
  var timestamp = new Date(doc._source["@timestamp"]).valueOf()

  for (var k in doc) {
    delete doc[k]
  }

  for (var k in item) {
      doc[k] = item[k]
  }

  doc.timestamp = timestamp
}

# 运行导出脚本即导出数据
$ sh run.sh

参考

  • https://github.com/elasticsearch-dump/elasticsearch-dump
  • https://help.aliyun.com/document_detail/461655.htm
  • https://www.cnblogs.com/resn/p/9082663.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈挨踢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值