elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解
1.如何实现mysql与elasticsearch的数据同步?
逐条转换为json显然不合适,需要借助第三方工具或者自己实现。核心功能点:同步增、删、改、查同步。
2、mysql与elasticsearch同步的方法有哪些?优缺点对比?
目前该领域比较牛的插件有:
1)、elasticsearch-jdbc,严格意义上它已经不是第三方插件。已经成为独立的第三方工具。https://github.com/jprante/elasticsearch-jdbc
2)、elasticsearch-river-mysql插件 https://github.com/scharron/elasticsearch-river-mysql
3)、go-mysql-elasticsearch(国内作者siddontang) https://github.com/siddontang/go-mysql-elasticsearch
1-3同步工具/插件对比:
go-mysql-elasticsearch仍处理开发不稳定阶段。
为什么选择elasticsearch-jdbc而不是elasticsearch-river-mysql插件的原因?(参考:http://stackoverflow.com/questions/23658534/using-elasticsearch-river-mysql-to-stream-data-from-mysql-database-to-elasticsea)
1)通用性角度:elasticsearch-jdbc更通用,
2)版本更新角度:elasticsearch-jdbc GitHub活跃度很高,最新的版本2.3.3.02016年5月28日兼容Elasticsearch2.3.3版本。
而elasticsearch-river-mysql 2012年12月13日后便不再更新。
综上,选择elasticsearch-jdbc作为mysql同步Elasticsearch的工具理所当然。
elasticsearch-jdbc的缺点与不足(他山之石):
1)、go-mysql-elasticsearch作者siddontang在博客提到的:
elasticsearch-river-jdbc的功能是很强大,但并没有很好的支持增量数据更新的问题,它需要对应的表只增不减,而这个几乎在项目中是不可能办到的。
http://www.jianshu.com/p/05cff717563c
2)、
博主leotse90在博文中提到elasticsearch-jdbc的缺点:那就是删除操作不能同步(物理删除)!
http://leotse90.com/2015/11/11/ElasticSearch与MySQL数据同步以及修改表结构/
我截止2016年6月16日没有测试到,不妄加评论。
3、elasticsearch-jdbc如何使用?要不要安装?
3.1 和早期版本不同点
elasticsearch-jdbcV2.3.2.0版本不需要安装。以下笔者使用的elasticsearch也是2.3.2测试。
操作系统:CentOS release 6.6 (Final)
看到这里,你可能会问早期的版本有什么不同呢?很大不同。从我搜集资料来看,不同点如下:
1)早期1.x版本,作为插件,需要安装。
2)配置也会有不同。
3.2 elasticsearch-jdbc使用(同步方法一)
前提:
1)elasticsearch 2.3.2 安装成功,测试ok。
2)mysql安装成功,能实现增、删、改、查。
可供测试的数据库为test,表为cc,具体信息如下:
mysql> select * from cc;
+----+------------+
| id | name |
+----+------------+
| 1 | laoyang |
| 2 | dluzhang |
| 3 | dlulaoyang |
+----+------------+
3 rows in set (0.00 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
第一步:下载工具。
址:http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.2.0/elasticsearch-jdbc-2.3.2.0-dist.zip
第二步:导入Centos。路径自己定,笔者放到根目录下,解压。unzip elasticsearch-jdbc-2.3.2.0-dist.zip
第三步:设置环境变量。
[root@5b9dbaaa148a /]# vi /etc/profile
export JDBC_IMPORTER_HOME=/elasticsearch-jdbc-2.3.2.0
使环境变量生效:
[root@5b9dbaaa148a /]# source /etc/profile
第四步:配置使用。详细参考:https://github.com/jprante/elasticsearch-jdbc
1)、根目录下新建文件夹odbc_es 如下:
[root@5b9dbaaa148a /]# ll /odbc_es/
drwxr-xr-x 2 root root 4096 Jun 16 03:11 logs
-rwxrwxrwx 1 root root 542 Jun 16 04:03 mysql_import_es.sh
2)、新建脚本mysql_import_es.sh,内容如下;
[root@5b9dbaaa148a odbc_es]# cat mysql_import_es.sh
’#!/bin/sh
bin=$JDBC_IMPORTER_HOME/bin
lib=$JDBC_IMPORTER_HOME/lib
echo '{
"type" : "jdbc",
"jdbc": {
"elasticsearch.autodiscover":true,
"elasticsearch.cluster":"my-application", #簇名,详见:/usr/local/elasticsearch/config/elasticsearch.yml
"url":"jdbc:mysql://10.8.5.101:3306/test", #mysql数据库地址
"user":"root", #mysql用户名
"password":"123456", #mysql密码
"sql":"select * from cc",
"elasticsearch" : {"host" : "10.8.5.101","port" : 9300
},
"index" : "myindex", #新的index
"type" : "mytype" #新的type
}
}'| java \-cp "${lib}/*" \-Dlog4j.configurationFile=${bin}/log4j2.xml \org.xbib.tools.Runner \org.xbib.tools.JDBCImporter
- 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
3)、为 mysql_import_es.sh 添加可执行权限。
[root@5b9dbaaa148a odbc_es]# chmod a+x mysql_import_es.sh
4)执行脚本mysql_import_es.sh
[root@5b9dbaaa148a odbc_es]# ./mysql_import_es.sh
第五步:测试数据同步是否成功。
使用elasticsearch检索查询:
[root@5b9dbaaa148a odbc_es]# curl -XGET 'http://10.8.5.101:9200/myindex/mytype/_search?pretty'
{"took" : 4,"timed_out" : false,"_shards" : {"total" : 8,"successful" : 8,"failed" : 0},"hits" : {"total" : 3,"max_score" : 1.0,"hits" : [ {"_index" : "myindex","_type" : "mytype","_id" : "AVVXKgeEun6ksbtikOWH","_score" : 1.0,"_source" : {"id" : 1,"name" : "laoyang"}}, {"_index" : "myindex","_type" : "mytype","_id" : "AVVXKgeEun6ksbtikOWI","_score" : 1.0,"_source" : {"id" : 2,"name" : "dluzhang"}}, {"_index" : "myindex","_type" : "mytype","_id" : "AVVXKgeEun6ksbtikOWJ","_score" : 1.0,"_source" : {"id" : 3,"name" : "dlulaoyang"}} ]}
}
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
出现以上包含mysql数据字段的信息则为同步成功。
4、 elasticsearch-jdbc 同步方法二
[root@5b9dbaaa148a odbc_es]# cat mysql_import_es_simple.sh
#!/bin/sh
bin=$JDBC_IMPORTER_HOME/bin
lib=$JDBC_IMPORTER_HOME/libjava \-cp "${lib}/*" \-Dlog4j.configurationFile=${bin}/log4j2.xml \org.xbib.tools.Runner \org.xbib.tools.JDBCImporter statefile.json[root@5b9dbaaa148a odbc_es]# cat statefile.json
{
"type" : "jdbc",
"jdbc": {
"elasticsearch.autodiscover":true,
"elasticsearch.cluster":"my-application",
"url":"jdbc:mysql://10.8.5.101:3306/test",
"user":"root",
"password":"123456",
"sql":"select * from cc",
"elasticsearch" : {"host" : "10.8.5.101","port" : 9300
},
"index" : "myindex_2",
"type" : "mytype_2"
}
}
- 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
脚本和json文件分开,脚本执行前先加载json文件。
执行方式:直接运行脚本 ./mysql_import_es_simple.sh 即可。
5、Mysql与elasticsearch等价查询
目标:实现从表cc中查询id=3的name信息。
1)MySQL中sql语句查询:
mysql> select * from cc where id=3;
+----+------------+
| id | name |
+----+------------+
| 3 | dlulaoyang |
+----+------------+
1 row in set (0.00 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2)elasticsearch检索:
[root@5b9dbaaa148a odbc_es]# curl http://10.8.5.101:9200/myindex/mytype/_search?pretty -d '
{
"filter" : { "term" : { "id" : "3" } }
}'
{"took" : 3,"timed_out" : false,"_shards" : {"total" : 8,"successful" : 8,"failed" : 0},"hits" : {"total" : 1,"max_score" : 1.0,"hits" : [ {"_index" : "myindex","_type" : "mytype","_id" : "AVVXKgeEun6ksbtikOWJ","_score" : 1.0,"_source" : {"id" : 3,"name" : "dlulaoyang"}} ]}
}
- 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
常见错误:
错误日志位置:/odbc_es/logs
日志内容:
[root@5b9dbaaa148a logs]# tail -f jdbc.log
[04:03:39,570][INFO ][org.xbib.elasticsearch.helper.client.BaseTransportClient][pool-3-thread-1] after auto-discovery connected to [{5b9dbaaa148a}{aksn2ErNRlWjUECnp_8JmA}{10.8.5.101}{10.8.5.101:9300}{master=true}]
Bug1、[02:46:23,894][ERROR][importer.jdbc ][pool-3-thread-1] error while processing request: cluster state is RED and not YELLOW, from here on, everything will fail!
原因:
you created an index with replicas but you had only one node in the cluster. One way to solve this problem is by allocating them on a second node. Another way is by turning replicas off.
你创建了带副本 replicas 的索引,但是在你的簇中只有一个节点。
解决方案:
方案一:允许分配‘它们’到第二个节点。
方案二:关闭副本replicas(非常可行)。如下:
curl -XPUT 'localhost:9200/_settings' -d '
{"index" : {"number_of_replicas" : 0}
}
- 1
- 2
- 3
- 4
- 5
- 6
‘
Bug2、[13:00:37,137][ERROR][importer.jdbc ][pool-3-thread-1] error while processing request: no cluster nodes available, check settings {autodiscover=false, client.transport.ignore_cluster_name=false, client.transport.nodes_sampler_interval=5s, client.transport.ping_timeout=5s, cluster.name=elasticsearch,
org.elasticsearch.client.transport.NoNodeAvailableException: no cluster nodes available, check
解决方案:
见上脚本中新增:
“elasticsearch.cluster”:”my-application”, #簇名,和/usr/local/elasticsearch/config/elasticsearch.yml 簇名保持一致。
参考:
http://stackoverflow.com/questions/11944915/getting-an-elasticsearch-cluster-to-green-cluster-setup-on-os-x
elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解相关推荐
- Elasticsearch 多语言及中文分词与检索详解
文章目录 1. 自然语言与查询 Recall 2. 混合多语言的挑战 3. 分词的挑战 4. 中文分词方法的演变 - 字典法 5. 中文分词方法的演变 - 基于统计法的机器学习算法 6. 中文分词器现 ...
- elasticsearch中best fields策略dis_max和tie_breaker详解
参考:elasticsearch中best fields策略dis_max和tie_breaker详解 概念 常规multi-field搜索结果分析 dis_max参数设置 一.为帖子数据增加cont ...
- JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- 关于Tomcat与MySQL连接池问题的详解
转载自 关于Tomcat与MySQL连接池问题的详解 研究了一天,终于有所收获,希望对大家有所帮助.首先请大家注意:这里尤其讨论Tomcat5.5版本中遇到的问题,为什么尤其单对这个版本,我一会儿 ...
- MySQL事务及隔离级别详解
MySQL事务及隔离级别详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的基本架构 MySQL的基本架构可以分为三块,即连接池,核心功能层,存储引擎层. 1> ...
- mysql cluster rpm包的作用_MySQL之——MySQL Cluster集群搭建详解(基于RPM安装包)
1.下载MySQL-cluster 7.3.7 http://dev.mysql.com/downloads/cluster/ 2.环境清理及安装 1)清理CentOS6.5自带的mysql服务,其中 ...
- linux 用root安装mysql数据库_Linux上安装Mysql及简单的使用详解
1. 安装mysql sudo apt-get update sudo apt-get install mysql-server sudo apt-get install python-mysqldb ...
- docker导入MySQL文件_Docker容器中Mysql数据的导入/导出详解
前言 Mysql数据的导入导出我们都知道一个mysqldump命令就能够解决,但如果是运行在docker环境下的mysql呢? 解决办法其实还是用mysqldump命令,但是我们需要进入docker的 ...
- php mysql无限_php+mysql实现无限分类实例详解
php+mysql实现无限分类实例详解 fenlei($arr[$i][0]); //$arr[$i][1]表示第$i+1个分类的id的值.进行递归,也就是把自己的id作为f_id参数把自己的子类 ...
- 深入mysql慢查询设置的详解
深入mysql慢查询设置的详解 在web开发中,我们经常会写出一些SQL语句,一条糟糕的SQL语句可能让你的整个程序都非常慢,超过10秒一般用户就会选择关闭网页,如何优化SQL语句将那些运行时间 比较 ...
最新文章
- PL/SQL学习笔记-常量变量及数据类型初步
- LeetCode Combination Sum IV(动态规划)
- 使用Wine 1.6.2 在OS X El Capitan下运行Galgame
- YunYang1994/tensorflow-yolov3 训练自己的数据集
- Go学习笔记—多线程
- 计算机二级web程序设计习题,2015年计算机二级《Web程序设计》习题及答案(5)
- Hibernate 连接不同数据库的方言
- 使用spark-shell从本地读取文件不成功的的操作。
- [WCF编程]10.操作:请求/应答操作
- css文件更新后浏览器网页仍然显示以前的页面解决方法
- 中国电信短信网关程序源码
- HELIOS模拟平台介绍
- 苹果iCloud服务疑遭中国黑客攻击
- mac mini调整屏幕亮度
- CCF论文列表(2022拟定)大更新!NAACL升B!ICLR继续陪跑...MICCAI空降B!PRCV空降C!
- android转ios 王者荣耀,王者荣耀怎么转移号
- 网络流中最大流和最小割算法
- 设计师必读的 10 本书
- 脑机接口(BCI)常用的实验范式
- 【网络与系统安全实验】网络监听及防御技术
热门文章
- ae saber插件_入门AE:影视特效中的黑洞是怎么做的?附带教程
- 自动化运维工具——ansible安装及模块介绍
- Shell脚本函数(函数传参、递归、创建库)
- python sort函数排序_Python中排序常用到的sort 、sorted和argsort函数
- 超声波测距的数据应该如何显示到七针oled上_一文读懂京东方、TCL华星、三星显示和LGD之间的复杂关系...
- java textfield事件_[求助]TextField失去焦点触发事件问题
- python中使用什么注释语句和运算_Python基础知识
- c语言试卷浙江理工大学杀人案件追踪,浙江理工大学c语言期末考试模拟试卷6 .pdf...
- mysql数据通讯方式_c# 与 Mysql 的通讯方式总结
- java环境变量设置的作用_JDK环境变量的配置及作用