同步mysql資料到ElasticSearch的最佳實踐
Elasticsearch是一個實時的分散式搜尋和分析引擎。它可以幫助你用前所未有的速度去處理大規模資料。ElasticSearch是一個基於Lucene的搜尋伺服器。它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放原始碼釋出,是當前流行的企業級搜尋引擎。設計用於雲端計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用方便。
安裝es以及head外掛,略。
為了使海量資料能夠提供實時快速的查詢,mysql很顯然力不從心,於是我們需要利用es提供大資料搜尋服務,典型的場景就是:產品或者商品搜尋。
首先是資料同步,將mysql資料同步到es的方式很多,經過測試,穩定且易用的是 logstash-input-jdbc
如何安裝logstash-input-jdbc外掛?
全量同步與增量同步
全量同步是指全部將資料同步到es,通常是剛建立es,第一次同步時使用。增量同步是指將後續的更新、插入記錄同步到es。(刪除記錄沒有辦法同步,只能兩邊執行自己的刪除命令)
根據公司內部實踐,logstash-input-jdbc增量同步的原理很簡單。我們做增量同步是需要知道插入和更新記錄的,因此,進入ES提供搜尋服務的表(要同步的標),都要加上update_time,每次插入和更新的時候更新這個欄位,讓logstash-input-jdbc知道即可。
詳見:
關鍵點:
where t.update_time > :sql_last_value
測試結果:
先更新一條資料看看
然後在es中查詢看看有沒有更新到
成功,自動同步了!
如果需要同時同步多個表,那麼需要以下配置
input { jdbc { jdbc_driver_library => "/Users/logstash/mysql-connector-java-5.1.39-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_connection_string => "jdbc:mysql://localhost:3306/database_name" jdbc_user => "root" jdbc_password => "password" schedule => "* * * * *" statement => "select * from table1" type => "table1" } jdbc { jdbc_driver_library => "/Users/logstash/mysql-connector-java-5.1.39-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_connection_string => "jdbc:mysql://localhost:3306/database_name" jdbc_user => "root" jdbc_password => "password" schedule => "* * * * *" statement => "select * from table2" type => "table2" } # add more jdbc inputs to suit your needs } output { elasticsearch { index => "testdb" document_type => "%{type}" # <- use the type from each input hosts => "localhost:9200" } }