日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

elasticsearch-jdbc同步myslq数据到elasticsearch

發布時間:2024/1/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。