MYSQL千万级别数据量迁移Elasticsearch5.6.1实战

从关系型库中迁移数据算是比较常见的场景,这里借助两个工具来完成本次的数据迁移,考虑到数据量并不大(不足两千万),未采用snapshot快照的形式进行。


Elasticsearch-jdbc,Github地址:https://github.com/jprante/elasticsearch-jdbc,从插件管方的兼容版本看,是不支持直接写入elasticsearch 5.6.1及更高版本中。

Release date JDBC Importer version Elasticsearch version
Aug 28 2016 2.3.4.1 2.3.4

不能直接写入es5,此处方案采用elasticsearch2.3.4做一个数据中转,(主因是前期采用的elasticsearch 2.3.4,本次升级才采用5.6.1)


Elasticsearch-dump,Github地址:https://github.com/taskrabbit/elasticsearch-dump,官方简介提示如下

Version 3.0.0 of Elasticdump has the default queries updated to only work for ElasticSearch version 5+. The tool may be compatible with earlier versions of Elasticsearch, but our version detection method may not work for all ES cluster topologies

Elasticdump是支持es2.x至es5.x版本的。


下面开始数据迁移数据。

1、准备工作

  1. 安装elasticsearch-jdbc,其依赖jvm环境,事先要准备好jvm环境。从官方下载压缩包,解压即可使用,解压后有两个目录(bin/lib),脚本存放在bin目录,下面有示例脚本供参考。安装过程参考官方文档。

  2. Elasticdump依赖node环境,所以需要事先安装Node环境,再通过npm命令安装。安装过程参考官方文档。

2、准备elasticsearch-jdbc迁移脚本

 
 
  1. #!/bin/sh

  2. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

  3. bin=${DIR}/../bin

  4. lib=${DIR}/../lib

  5. echo '

  6. {

  7.    "type": "jdbc",

  8.    "jdbc": {

  9.        "elasticsearch.autodiscover": true,

  10.        "url": "jdbc:mysql://192.168.1.102:3306/test",

  11.        "user": "root",

  12.        "password": "root",

  13.        "sql": "select *,tid as _id from tb_db_info",

  14.        "elasticsearch": {

  15.            "host": "192.168.12.1",

  16.            "port": 9300

  17.        },

  18.        "elasticsearch.cluster":"my-application",

  19.        "index": "testIndex",

  20.        "type": "testType"

  21.    }

  22. }

  23. ' | java \

  24.    -cp "${lib}/*" \

  25.    -Dlog4j.configurationFile=${bin}/log4j2.xml \

  26.    org.xbib.tools.Runner \

  27.    org.xbib.tools.JDBCImporter

脚本内容很容易理解,执行此脚本,查看数据是否从mysql中写入elasticsearch索引库中。

  • 建议以后台挂起的方式执行任务,防止连接中断导致任务中断。

  • 若对目标索引有特殊要求,比如某些字段不进行analyze等,可提前建立好索引及映射机制,再使用脚本进行数据导入工作。

3,采用Elasticdump迁移数据

从官方的介绍中可以看到,大致分三个步骤:

 
 
  1. # Copy an index from production to staging with analyzer and mapping:

  2. elasticdump \

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

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

  5.  --type=analyzer

  6. elasticdump \

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

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

  9.  --type=mapping

  10. elasticdump \

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

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

  13.  --type=data

为提高脚本的执行效率,特殊关注下limit参数,数据批的大小,默认是100条,比较小的,这个需要根据具体的环境来调整,建议1000以上。脚本执行过程中会有日志输出,显示数据传输的进程。

  • 若对目标索引需要特殊处理的情况,而不采用上面两个脚本——type类型为:analyzer及mapping,制定好自己的索引及映射关系后,再使用工具进行迁移。

  • 为应对脚本针对大数据量的迁移执行中断的情况,工具中有参数offset,但只针对写索引有效,并不能按我们的预期直接从offset中断处继续读中断后的数据进而去迁移数据,而是继续从头开始,此处需要特别注意。

  • limit参数不宜调试过大,容易导致timeout的情况发生,近而导致任务中断,执行失败。

  • 脚本运行建议以后台挂起的形式运行,防止因连接中断导致任务中断。

实际操作时请结合自身的具体环境,希望能帮到你。

扩展阅读:

猜你喜欢

转载自blog.csdn.net/hero272285642/article/details/79297321