elasticsearch 数据迁移

第一种方法 (使用elasticdump)

  这是一个nodejs的插件,安装方式直接使用 npm即可

  导出索引的格式

#格式:elasticdump --input {protocol}://{host}:{port}/{index} --output ./test_index.json
#例子:将ES中的test_index 中的索引导出
#导出当前索引的mapping结构
$ elasticdump --input http://192.168.56.104:9200/test_index --output ./test_index_mapping.json --type=mapping
#导出当前索引下的所有真实数据
$ elasticdump --input http://192.168.56.104:9200/test_index --output ./test_index.json --type=data

  向新的es服务导入索引

# 创建索引
$ curl -XPUT http:192.168.56.104:9200/test_index
#因为导入的是mapping,所以设置type为mapping
$ elasticdump --input ./test_index_mapping.json --output http://192.168.56.105:9200/ --type=mapping
#因为导入的是data(真实数据)所以设置type为data
$ elasticdump --input ./test_index.json --output http://192.168.56.105:9200/ --type=data

  

使用 elasticdump的官方docker镜像进行数据的导入导出

# 镜像下载
$ docker pull taskrabbit/elasticsearch-dump
# 下面还是例子:通过镜像导出数据到本地
# 创建一个文件夹用于保存导出数据
$ mkdir -p /root/data
# 下面需要对路径进行映射并执行命令(导出mapping)
$ docker run --rm -ti -v /data:/tmp taskrabbit/elasticsearch-dump \
  --input=http://production.es.com:9200/my_index \
  --output=/tmp/my_index_mapping.json \
  --type=mapping
# 导出(data)
$ docker run --rm -ti -v /root/data:/tmp taskrabbit/elasticsearch-dump \
  --input=http://192.168.56.104:9200/test_index \
  --output=/tmp/elasticdump_export.json \
  --type=data
  -----------------------------------------------------------------------------
# 以下内容为ES -> ES的数据迁移例子
$ docker run --rm -ti taskrabbit/elasticsearch-dump \
  --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=mapping
$ docker run --rm -ti taskrabbit/elasticsearch-dump \
  --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=data

  

注:上面的这些导入导出都是最基本的使用,当然还有很多高级用法,参考下面所列出来的命令进行尝试或者直接访问Github官网,查看更加详细的说明,这里只作为记录!

 

logstash系列一使用logstash迁移ES数据

es的文档id生成方式可以是 自动的uuid,也可以是自定义的id,可以用业务中的id字段进行映射

自动的id,URL安全,base64编码,GUID,如下:

POST /test_index/test_type 
{ "test_content": "my test" }

 手动的就靠自己定义

PUT /test_index/test_type/2
{
  "test_content": "my test"
}

  

那么使用logstash抽取数据是怎么做映射的?配置文件如下:

input {
    elasticsearch {
        hosts => ["192.168.0.1:9200"]
        index => "ddd"
        query => '{ "query": {"match_all" : {} } }'
        size => 1000
        scroll => "1m"
        codec => "json"
        docinfo => true
    }
}

output {
    stdout {
      codec => rubydebug
    }
    elasticsearch {
        hosts => ["192.168.0.2:9200"]
        document_type => "messages"
        document_id => "%{id}" 
        index => "ddd"
    }
}

  

document_id => "%{id}"  指定 文档id 使用自己定义json 文件中的id 号映射。
 
 

 

posted @ 2021-02-01 11:28  rianley  阅读(4459)  评论(0编辑  收藏  举报