elasticsearch-jdbc同步myslq数据到elasticsearch
来源:互联网 发布:网络cry是什么意思啊 编辑:程序博客网 时间:2024/06/05 07:45
一、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/shbin=$JDBC_IMPORTER_HOME/binlib=$JDBC_IMPORTER_HOME/libecho '{"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/shbin=$JDBC_IMPORTER_HOME/binlib=$JDBC_IMPORTER_HOME/lib java \ -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
- 用elasticsearch-river-jdbc同步数据到elasticsearch
- 用elasticsearch-river-jdbc同步数据到elasticsearch
- Elasticsearch-jdbc数据同步配置
- logstash jdbc同步mysql多表数据到elasticsearch
- elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解
- elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解
- elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解
- elasticsearch之jdbc同步
- elasticsearch之jdbc同步
- HBase数据同步到ElasticSearch的方案
- 通过HBase Observer同步数据到ElasticSearch
- mysql中的数据同步到elasticsearch
- 通过HBase Observer同步数据到ElasticSearch
- 实时同步MySQL数据到Elasticsearch
- MongoDB 数据自动同步到 ElasticSearch
- 如何同步TableStore数据到Elasticsearch
- Elasticsearch系列(九)----使用Logstash-input-jdbc同步数据库中的数据到ES
- contos7.0 jdk1.8 安装
- 利用百度云m3u8格式hls直播实例
- 100 个 Linux 常用命令大全
- GitHub访问不了
- C/C++对URL分割进行实现
- elasticsearch-jdbc同步myslq数据到elasticsearch
- python: hasattr()、setattr()、getattr()、delattr() 内建函数
- Linux常用的命令
- 手机访问PC网站自动跳转到手机网站代码
- Springboot从易到难(基本页面显示到框架整合、数据交互)
- spring mvc rocketMq简单配置
- 怎样根据使用环境需求来选择不同参数的光模块
- 国庆假期微信大数据报告
- 用户端接收解析JSON数据