實現MySQL資料同步到ElasticSearch中
阿新 • • 發佈:2018-12-16
一、場景
筆者最近在寫全文搜尋引擎服務,用到的是ElasticSearch框架,該篇部落格就如何實現MySQL資料同步到ES中進行闡述
二、解決方案
執行系統:mac系統下
採用到的技術:ES2.4、logstash-2.4.1
用到的相關外掛:ES-head、logstash-input-jdbc、logstash-output-elasticsearch
其安裝該篇部落格就不多做闡述。
讀者安裝完以上的相關外掛後,進入logstash-2.4.1目錄下,建立config資料夾,在config資料夾下建立logstash-mysql.conf並下載mysql-connector-java-5.1.39.jar包如圖(還有:再建立個logs目錄,來放日誌)
logstash-mysql.conf中的內容如下
input { jdbc { jdbc_driver_library => "./config/mysql-connector-java-5.1.39.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" # 資料庫相關配置 jdbc_connection_string => "jdbc:mysql://localhost:3306/wcspider?characterEncoding=UTF-8&useSSL=false" jdbc_user => "root" jdbc_password => "1111" statement => "SELECT * FROM shiyuemami limit 0,1" jdbc_paging_enabled => "true" jdbc_page_size => "50000" schedule => "* * * * *" } } filter { json { source => "message" remove_field => ["message"] } } output { stdout { codec => rubydebug } elasticsearch { hosts => "localhost" #將mysql資料加入myindex索引下,會自動建立 index => "myindex" } }
完成後,記得啟動es : brew services start [email protected]
然後進入logstash-2.4.1目錄下:輸入命令:nohup bin/logstash -f config/logstash-mysql.conf > logs/logstash.out &
如果要停止該檔案執行,可以直接kill掉程序,程序號會在執行上面的命令時打印出來
可以看到logs目錄下生成logstash.out
開啟logstash.out會看到同步到es的相關資料
開啟http://localhost:9200/_plugin/head/