分类:
版权声明:本文为博主原创文章,地址:http://blog.csdn.net/napoay,转载请留言.
ElasticSearch同步MySQL的插件选择了elasticsearch-jdbc,理由是活跃度高,持续更新,最新版本兼容elasticsearch-2.3.3.
一、下载
下载地址:https://github.com/jprante/elasticsearch-jdbc
下载后解压,里面有bin、lib2个目录.
二、mysql配置
确保mysql能用,在mysql中新建一个test数据库
mysql>create database test;
新建一张user表
mysql> create table user(id int(10) Not null,name char(10));
插入几条数据.
mysql> insert into test values("1","zhangsan");
mysql> insert into user values("2","LiSi"); mysql> insert into user values("3","WangWu"); mysql> insert into user values("4","MaLiu");
查看所有数据:
mysql> select * from user;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | LiSi |
| 3 | WangWu |
| 4 | MaLiu |
+----+----------+
4 rows in set (0.00 sec)
这样mysql中的数据就准备好了.
三、导入数据
新建一个odbc_es文件夹,新建mysql_import_es.sh
脚本,脚本内容:
bin=/Users/yaopan/Documents/bropen/elasticsearch-jdbc-2.3.2.0/bin
lib=/Users/yaopan/Documents/bropen/elasticsearch-jdbc-2.3.2.0/lib
echo '{
"type" : "jdbc", "jdbc" : { "elasticsearch.autodiscover":true, "elasticsearch.cluster":"bropen", "url" : "jdbc:mysql://localhost:3306/test", "user" : "root", "useSSL":"true", "password" : "123456", "sql" : "select *, id as _id from user", "elasticsearch" : { "host" : "127.0.0.1", "port" : 9300 }, "index" : "test", "type" : "user" } }' | java \ -cp "${lib}/*" \ -Dlog4j.configurationFile=${bin}/log4j2.xml \ org.xbib.tools.Runner \ org.xbib.tools.JDBCImporter
其中bin和lib用了绝对路径.
添加可执行权限:
chmod a+x mysql_import_es.sh
执行脚本:
./mysql_import_es.sh
报了一个SSL连接的警告,没有错误.如果出现Error: Could not find or load main class org.xbib.tools.Runner
之类的错误,很有可能是bin和lib路径出现问题.
查看导入结果:
http://localhost:9200/test/user/_search?pretty
head插件中查看:
参考文章
ElasticSearch同步Mysql
elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解