elasticsearch-jdbc同步myslq数据到elasticsearch
一、linux上使用
前提:
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)
第一步:下载工具。
址: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
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"}} ]}
}
出现以上包含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"
}
}
脚本和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)
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"}} ]}
}
二、windoes上使用
脚本配置:
@echo offset LIB=%JDBC_IMPORTER_HOME%\lib\*
set BIN=%JDBC_IMPORTER_HOME%\binecho {^"type" : "jdbc",^"jdbc" : {^"url" : "jdbc:mysql://localhost:3306/test",^"user" : "root",^"password" : "esri",^"sql" : "select * from mysql2es_test",^"treat_binary_as_string" : true,^"elasticsearch" : {^"cluster" : "application",^"host" : "localhost",^"port" : 9300^},^"index" : "test"^}^
}^ | "%JAVA_HOME%\bin\java" -cp "%LIB%" -Dlog4j.configurationFile="%BIN%\log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"
elasticsearch-jdbc同步myslq数据到elasticsearch相关推荐
- 使用canal实时同步MySQL数据到Elasticsearch
使用canal实时同步MySQL数据到Elasticsearch 搭建环境 安装 elasticsearch 安装 kibana 下载和安装canal 1.下载canal 2.配置MySQL 3.配置 ...
- Logstash 安装及简单实用(同步MySql数据到Elasticsearch)
Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的.多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件 Windows环境: 1.下载logstas ...
- 使用canal同步MySQL数据到Elasticsearch(ES)
目录 1.功能及使用场景 1.1.功能介绍 1.2.使用场景 2.需求引入 3.canal文件下载及准备 3.1 下载文件 3.2 准备文件 4.deployer安装及效果测试 4.1.deploye ...
- canal deployer+canal adapter自动同步MySQL数据到ElasticSearch
一.安装Elastic Search 6.8 1.下载 Elastic Search 6.8资源下载 2.部署环境配置 创建用户(es不能使用root启动,否则报错) adduser elastic ...
- logstash同步mysql数据到elasticsearch,动态模板
后台运行logstash Last login: Thu Jul 23 17:50:26 2020 [root@localhost ~]# nohup /home/essoft/logstash-6 ...
- 【Elasticsearch】推荐一个同步Mysql数据到Elasticsearch的工具
1.概述 转载:https://elasticsearch.cn/article/756
- MySQL从零到一解读增量同步数据到elasticsearch canal adapter方式(binlog)实现
本文是作者在单机上面从零到一实现增量同步MySQL数据到elasticsearch canal adapter方式(binlog)实现. 实现步骤 (1)安装MySQL (2)开启MySQL binl ...
- 基于HBase+ ElasticSearch的海量交通数据实时存取方案设计
董长青,任女尔,张庆余,田玉靖 北京卡达克数据技术中心软件业务本部,天津 300300 摘要:交通流数据具有数据海量.存储和交互速率快等特征,因此其数据的采集.存储及检索成为了车辆远程监控平台中的关键 ...
- Logstash同步mysql数据
Logstash同步mysql数据到ElasticSearch 1.安装logstash 下载logstash,解压压缩包 下载地址:https://www.elastic.co/downloads/ ...
最新文章
- 你用什么态度去看待世界,你就会得到什么样的世界
- [YTU]_2618 ( B 求类中数据成员的最大值-类模板)
- 用MySQL创建数据库和数据库表
- gitee怎么看用户名_教你手机怎么远程连接云服务器
- 物料凭证不产生会计凭证的几种情况
- h5实现网页内容跟随窗口大小移动_HTML5 移动页面自适应手机屏幕四类方法
- Filecoin: 影响力容错(PFT)和预期共识(EC)
- 【二分图判定】hdu3478 Catch
- 第八届 蓝桥杯 承压计算
- tomcat安装与项目部署
- A20 文件系统预装APK
- find_first_of()和 find_last_of()
- 贝莱德COO:作为全球最大资产管理公司,为什么说我们还是成长中的科技公司?
- 单片机C语言基础知识篇
- ev3pid巡线_基于LEGO使用PID算法进行单光巡线机器人设计的心得体会
- 最小二乘法计算CCM
- web网页保存为PDF文件
- Poi 、Jacob 统计word文档字数实现方式
- php汉字转换拼音,php实现汉字转拼音
- 暑假教师计算机培训总结,教师信息化培训心得(精选5篇)
热门文章
- leetcode算法刷题记录表
- Mysql和Oracle获取自增主键
- 第一个linux桌面,Ubuntu 4.10 “Warty Warthog”:回顾第一个Ubuntu Linux桌面
- 模拟k8s项目的生命周期
- OpenStack 的部署T版(二)——Keystone组件
- DHCP和DHCP中继功能与配置
- synchronized原理_synchronized关键字的作用、原理以及锁优化
- ubuntu 20 安装vnc_Windows 远程控制 Ubuntu 系统
- 华三服务器怎么设置系统启动模式,H3C 开局设置
- nodejs shell交互_nodejs调用shell