1.工具安裝
下載數據導入工具包 elasticsearch-jdbc-2.3.4.1-dist.zip,可以到官方網站上下載,
由於github上是隻有原代碼,沒有編譯好的可執行包,可以到版本庫上[下載] (http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/) 相應的版本
注意elasticsearch-jdbc版本與 elasticsearch要對應上,具體版本對應可以到github上看
https://github.com/jprante/elasticsearch-jdbc
下載
wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/
如果這種方式下載慢,也可以本地下載後上傳
得到的elasticsearch-jdbc-2.3.4.1-dist.zip 的包可以解壓到任何目錄,elasticsearch-jdbc是一個獨立的項目,並不是原來的老版本需要導入elasticsearch的plugins目錄中,作爲elasticsearch的插件安裝
unzip elasticsearch-jdbc-2.3.4.1-dist.zip
解壓得到目錄
/bin
/lib
這樣就安裝成功了
注意如果你導入的數據庫不是mysql數據庫,那你需要在lib目錄中放入相應數據庫的驅動包,因爲elasticsearch-jdbc默認就有mysql的驅動,所以數據庫是mysql就不用了
2.編寫導入腳本
可以進入bin目錄下,裏面很多數據導入腳本的範例腳本,可以稍做修改就可以用了,例如mysql-blog.sh 就是mysql數據庫導入的例子
我的導入腳本如下
#!/bin/sh bin=/opt/elasticsearch-jdbc-2.3.4.1/bin lib=/opt/elasticsearch-jdbc-2.3.4.1/lib echo ' { "type" : "jdbc", "jdbc" : { "url" : "jdbc:mysql://10.3.5.21:3306/998pz?useUnicode=true&characterEncoding=utf-8", "user" : "root", "password" : "roottest", "sql" : "select p.name
,p.type from prompt p;", "index" : "prompt", "type" : "prompt", "metrics": { "enabled" : true }, "elasticsearch" : { "cluster" : "es-cluster", "host" : "10.3.32.83", "port" : 9300 } } } ' | java \ -cp "${lib}/*" \ -Dlog4j.configurationFile=${bin}/log4j2.xml \ org.xbib.tools.Runner \ org.xbib.tools.JDBCImporter
該腳本將表prompt的數據導入到索引prompt中去
注意:導入的字段名一定要跟索引的字段名一樣
腳本編輯完成後需要賦予文件相應的執行權限
chmod 777 prompt.sh
如果導入腳本是你本地windows上編輯好上傳到服務器,可能會因爲windows編碼與linux編碼不一致,出現莫名其妙的錯誤,可以用vi編輯進入文件
在命令模式下,轉換相應的編碼
set ff=unix
3.導入地理座標數據
索引
"location": {"store": true,"type": "geo_point"}
腳本
#!/bin/sh bin=/opt/elasticsearch-jdbc-2.3.4.1/bin lib=/opt/elasticsearch-jdbc-2.3.4.1/lib echo ' { "type" : "jdbc", "jdbc" : { "url" : "jdbc:mysql://10.3.5.21:3306/998pz?useUnicode=true&characterEncoding=utf-8", "user" : "root", "password" : "roottest", "sql" : "SELECT bh.,bh.name as nameSuggest,bh.latitude AS \"location.lat\", bh.longitude as \"location.lon\" FROM bd_hospital bh;", "index" : "hospital2", "type" : "hospital2", "metrics": { "enabled" : true }, "elasticsearch" : { "cluster" : "es-cluster", "host" : "10.3.32.83", "port" : 9300 } } } ' | java \ -cp "${lib}/" \ -Dlog4j.configurationFile=${bin}/log4j2.xml \ org.xbib.tools.Runner \ org.xbib.tools.JDBCImporter
學習視頻
複製鏈接,在瀏覽器打開
tomcat源碼解析
https://study.163.com/course/introduction/1209535854.htm
Springmvc源碼解析
https://study.163.com/course/introduction/1209536851.htm
dubbo源碼解析
https://study.163.com/course/introduction/1209648816.htm