使用logstash 同步數據庫到es

1.上傳logstash-6.4.3.tar.gz到服務中

2.tar –zxvf  logstash-6.4.3.tar.gz

3.cd logstash-6.4.3 

4. bin/logstash-plugin install logstash-input-jdbc

5. bin/logstash-plugin install logstash-output-elasticsearch

 

新寫一個mysql.conf 放到 logstash-6.4.3 這個裏頭

input {
  jdbc {
    jdbc_driver_library => "/Users/liyaochu/important/mysql-connector-java-5.1.46.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=UTF-8"
    jdbc_user => "root"
    jdbc_password => "root"
    schedule => "* * * * *"
    statement => "SELECT * FROM user WHERE update_time >= :sql_last_value"
    use_column_value => true
    tracking_column_type => "timestamp"
    tracking_column => "update_time"
    last_run_metadata_path => "syncpoint_table"
    type => "jdbc"
  }
}


output {
    elasticsearch {
        # ES的IP地址及端口
        hosts => ["localhost:9200"]
        # 索引名稱 可自定義
        index => "user"
        # 需要關聯的數據庫中有有一個id字段,對應類型中的id
        document_id => "%{id}"
        document_type => "user"
    }
    stdout {
        # JSON格式輸出
        codec => json_lines
    }
}

最後

上傳/Users/liyaochu/important/mysql-connector-java-5.1.46.jar" 需要這個jia包

 

./bin/logstash -f mysql.conf 啓動

二.多文件方式同步ES數據

一個 logstash 實例可以藉助 pipelines 機制同步多個表,只需要寫多個配置文件就可以了,假設我們有兩個表 table1 和 table2,對應兩個配置文件 sync_table1.cfg 和 sync_table2.cfg

在 config/pipelines.yml 中配置

- pipeline.id: table1

  path.config: "config/mysql.conf"

- pipeline.id: table2

  path.config: "config/mysql.2conf"

 

./bin/logstash

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章