elasticsearch-jdbc同步myslq数据到elasticsearch
一、linux上使用
前提:
1)elasticsearch 2.3.2 安裝成功,測試ok。
2)mysql安裝成功,能實現增、刪、改、查。
可供測試的數據庫為test,表為cc,具體信息如下:
第一步:下載工具。
址: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,內容如下;
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檢索查詢:
出現以上包含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語句查詢:
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上使用
腳本配置:
總結
以上是生活随笔為你收集整理的elasticsearch-jdbc同步myslq数据到elasticsearch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JVM中的逃逸分析
- 下一篇: 机器学习里面的树形模型