Elasticsearch索引迁移的四种方式

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 本文主要讲解Elasticsearch下实现索引迁移的几种方式。#0、引言

image.png链接

本文主要讲解Elasticsearch下实现索引迁移的几种方式。

#0、引言

将ES中的索引拷贝到其他ES中,或者将ES整体迁移,研究发现有两个开源的工具:elaticserch-dump和 Elasticsearch-Exporter。

除此之外,logstash在索引同步、迁移方面的作用也很大。

两工具及logstash实现迁移的介绍、安装、使用、验证效果等展示如下:


#1、elasticsearch-dump迁移

##1.1 elasticsearch-dump简介

Tools for moving and saving indicies. 从来移动和保存索引的工具。

https://github.com/taskrabbit/elasticsearch-dump


##1.2 elasticsearch-dump安装


1) yum install epel-release

2) yum install nodejs

3) yum install npm

4) npm install elasticdump

5) cd node_modules/elasticdump/bin  后便可以执行操作。

1

2

3

4

5

安装后如下所示:


[root@N3 elasticdump]# pwd

/home/tp/node_modules/elasticdump

[root@N3 elasticdump]# ls -al

total 388

drwxr-xr-x 2 root root 4096 Mar 21 15:46 bin

-rw-r--r-- 1 root root 174 Mar 18 2016 Dockerfile

-rw-r--r-- 1 root root 299251 Mar 15 2014 elasticdump.jpg

-rw-r--r-- 1 root root 6172 Feb 2 23:47 elasticdump.js

drwxr-xr-x 2 root root 4096 Jul 13 2016 .github

drwxr-xr-x 3 root root 4096 Mar 21 15:46 lib

-rw-r--r-- 1 root root 11356 May 22 2014 LICENSE.txt

drwxr-xr-x 10 root root 4096 Mar 21 15:46 node_modules

-rw-r--r-- 1 root root 44 May 22 2014 .npmignore

-rw-r--r-- 1 root root 15135 Mar 21 15:46 package.json

-rw-r--r-- 1 root root 13335 Dec 14 06:20 README.md

drwxr-xr-x 3 root root 4096 Mar 21 15:46 test

-rw-r--r-- 1 root root 1150 Dec 2 07:54 .travis.yml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

##1.3 elasticsearch-dump 使用


'#拷贝analyzer如分词

elasticdump \

 --input=http://production.es.com:9200/my_index \

 --output=http://staging.es.com:9200/my_index \

 --type=analyzer

'#拷贝映射

elasticdump \

 --input=http://production.es.com:9200/my_index \

 --output=http://staging.es.com:9200/my_index \

 --type=mapping

'#拷贝数据

elasticdump \

 --input=http://production.es.com:9200/my_index \

 --output=http://staging.es.com:9200/my_index \

 --type=data

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

##1.4 elasticsearch-dump实战小结

源ES版本1.6.0,目标ES版本:2.3.4,验证发现:analyzer和mapping可以拷贝成功。

但是,data拷贝不成功。目标机器ES中不能显示出数据。根本原因没有排查到。


#2、 Elasticsearch-Exporter迁移

##2.1 Elasticsearch-Exporter简介

https://github.com/mallocator/Elasticsearch-Exporter

A small script to export data from one Elasticsearch cluster into another. 将ES中的数据向其他导出的简单脚本实现。


##2.2、Elasticsearch-Exporter安装

http://www.dahouduan.com/2014/12/25/centos-yum-install-nodejs-npm/

centos用 yum 方式安装 nodejs 和 npm


npm install nomnom

npm install colors

npm install elasticsearch-exporter --production

1

2

3

安装后:


[root@N3 elasticsearch-exporter]# ll -ls

total 80

4 drwxr-xr-x 2 root root 4096 Mar 21 22:01 drivers

12 -rw-r--r-- 1 root root 11523 Sep 19 2014 exporter.js

12 -rw-r--r-- 1 root root 11324 Mar 16 2014 LICENSE

4 drwxr-xr-x 4 root root 4096 Mar 21 22:01 node_modules

12 -rw-r--r-- 1 root root 11259 Sep 19 2014 options.js

16 -rw-r--r-- 1 root root 14500 Mar 21 22:01 package.json

16 -rw-r--r-- 1 root root 12645 Sep 19 2014 README.md

4 drwxr-xr-x 2 root root 4096 Apr 25 2014 tools


1

2

3

4

5

6

7

8

9

10

11

##2.3、 Elasticsearch-Exporter使用


node exporter.js -a <source hostname> -b <target hostname> -p <s port> -q <t port> -i <s index> -j <t index>

1

即可实现跨机器索引的迁移。


更多的参数可以查看node exporter.js --help


[root@N3 elasticsearch-exporter]# node exporter.js --help

Elasticsearch Exporter - Version 1.4.0

Usage: exporter [options]

Options:

 -a <hostname>, --sourceHost <hostname>  迁移源机器地址

 -b <hostname>, --targetHost <hostname>  迁移目的机器地址(如果没有设置索引,目的地址需要有别于源地址)

 -p <port>, --sourcePort <port>   源机器的ES的端口,9200(一般)

 -q <port>, --targetPort <port>    目标机器的ES的端口,9200(一般)


 -i <index>, --sourceIndex <index> 源ES待导出的索引,如果该值不设定,整个的数据库都会导出。

 -j <index>, --targetIndex <index>目标机器ES的索引,如果源索引设定,该值必须填写。

1

2

3

4

5

6

7

8

9

10

11

##2.4、 Elasticsearch-Exporter 索引迁移实战(验证ok)


[root@No3 elasticsearch-exporter]# node exporter.js -a 10.221.110.31-b 100.0.1.130 -p 9200 -q 9200 -i awppx -j awppx

同步最后会显示:

Number of calls: 169

Fetched Entries: 8064 documents

Processed Entries: 8064 documents

Source DB Size: 8064 documents

1

2

3

4

5

6

代表同步成功。

源ES版本1.6.0,目标ES版本:2.3.4,

验证发现:可以使用 Elasticsearch-Exporter跨机器、跨ES版本同步索引成功。


#3、logstash定向索引迁移


[root@N3 bin]# cat ./logstash_output_mongo/logstash_es22es.conf

input {

 elasticsearch {

 hosts => [ "100.200.10.54:9200" ]

 index => "doc"

 size => 1000

 scroll => "5m"

 docinfo => true

 scan => true

 }

}


filter {

json {

 source => "message"

 remove_field => ["message"]

 }

 mutate {

 # rename field from 'name' to 'browser_name'

 rename => { "_id" => "wid" }

}

}


output {

 elasticsearch {

 hosts => [ "100.20.32.45:9200" ]

 document_type => "docxinfo"

 index => "docx"

 }


 stdout {

 codec => "dots"

 }


}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

4、elasticsearch-migration工具

https://github.com/medcl/elasticsearch-migration

支持多个版本间的数据迁移,使用scroll+bulk

1.版本支持1.x,2.x.5.0 (0.x未测试)

2.支持http basic auth 认证的es集群

3.支持导入覆盖索引名称(目前只支持单个索引导入的情况下可指定)

4.支持index setting和mapping的同步(相关es大版本,2.x和5.0之间不支持)

5.支持dump到本地文件

6.支持从dump文件加载导入到指定索引

讨论参考:https://elasticsearch.cn/article/78


5、小结

对比发现, Elasticsearch-Exporter在索引迁移方面相对更好用。(待深入研究补充)

而logstash定向索引用于辅助解决 Elasticsearch-Exporter不同版本迁移有Bug的情形。


参考:

[1]http://blog.csdn.net/u014587343/article/details/50541494

[2]http://stackoverflow.com/questions/26547560/how-to-move-elasticsearch-data-from-one-server-to-another

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
28天前
|
存储 自然语言处理 关系型数据库
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
38 0
|
2月前
|
存储 算法 NoSQL
Elasticsearch拆分索引知多少
Elasticsearch拆分索引知多少
33 0
|
1月前
|
JSON 监控 数据管理
【Elasticsearch专栏 12】深入探索:Elasticsearch使用索引生命周期管理(ILM)自动化删除旧数据
Elasticsearch的ILM功能允许用户定义策略,自动管理索引从创建到删除的生命周期。用户可以设置策略,根据索引年龄或大小自动删除旧数据,节省存储空间。通过应用ILM策略于索引模板,新索引将遵循预定义的生命周期。用户还可以监控ILM状态,确保策略按预期执行。使用ILM,用户可以高效地管理数据,确保旧数据及时删除,同时保持数据完整性和安全性。
|
2月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 01】深入探索:Elasticsearch的正向索引和倒排索引是什么?
正向索引根据文档ID直接查找文档内容,适用于精确匹配场景;而倒排索引则基于文档内容构建,通过关键词快速定位相关文档,适用于全文搜索,显著提高查询效率,是搜索引擎的核心技术。
|
1天前
|
安全 API 数据安全/隐私保护
Elasticsearch 通过索引阻塞实现数据保护深入解析
Elasticsearch 通过索引阻塞实现数据保护深入解析
|
2月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 02】深入探索:Elasticsearch为什么使用倒排索引而不是正排索引
倒排索引在搜索引擎中更受欢迎,因为它直接关联文档内容,支持全文搜索和模糊搜索,提高查询效率。其紧凑的结构减少了存储空间,并方便支持多种查询操作。相比之下,正排索引在搜索效率、存储和灵活性方面存在局限。
|
2月前
|
API 索引
Elasticsearch Index Shard Allocation 索引分片分配策略
Elasticsearch Index Shard Allocation 索引分片分配策略
77 1
|
2月前
|
存储 自然语言处理 关系型数据库
Elasticsearch创建一个索引怎么也这么复杂
Elasticsearch创建一个索引怎么也这么复杂
41 0
|
2月前
|
Java 网络架构 索引
Elasticsearch 如何实现索引的伪·命名空间
Elasticsearch 如何实现索引的伪·命名空间
9 0
|
2月前
|
存储 Java API
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧———索引与数据上传(二)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧———索引与数据上传(二)

热门文章

最新文章