Logstash7.6.2同步Mysql數據到ElasticSearch

一、準備工做:存在的mysql的數據庫表、建立好的elasticsearch索引java

二、下載mysql-connectormysql

下載地址:https://dev.mysql.com/downloads/connector/j/ sql

選擇 PlatForm Independent數據庫

下載後解壓後獲得connector的jar包 json

三、新建配置文件 sync_tablename.conf,tomcat

input {
    stdin{
    }
    jdbc {
      # 鏈接的數據庫地址和數據庫,指定編碼格式,禁用SSL協議,設定自動重連
      jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/userdb?characterEncoding=UTF-8&useSSL=false&autoReconnect=true"
      # 用戶名密碼
      jdbc_user => "user"
      jdbc_password => "password"
      # jar包的位置
      jdbc_driver_library => "/u02/tomcat/mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar"
      # mysql的Driver
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      jdbc_default_timezone => "Asia/Shanghai"
      jdbc_paging_enabled => "true"
      jdbc_page_size => "10"
      #statement_filepath => "config-mysql/test.sql"
      #注意這個sql不能出現type,這是es的保留字段
      statement => "select * from user where name='張三' "
      schedule => "* * * * *"
    }
}
output {
    elasticsearch {
        hosts => "127.0.0.1:9200"
        # index名
        index => "user"
        # type名
        document_type => "_doc"
        # 須要關聯的數據庫中有有一個id字段,對應索引的id號
        document_id => "%{id}"
    }
    stdout {
        codec => json_lines
    }
}

啓動報錯記錄1: [logstash.config.source.local.configpathloader] No config files found in path {:path=>"/u02/tomcat/logstash-7.6.2/bin/config/sync_tablename.conf"}                     ./logstash -f config/sync_tablename.confapp

        報錯緣由:啓動的時候找不到指定路徑elasticsearch

        解決方法:能夠放在bin目錄下,啓動時不須要指定路徑;          ./logstash -f  sync_tablename.conf編碼

啓動報錯記錄2:[FATAL][logstash.runner          ] The given configuration is invalid. Reason: Expected one of [ \t\r\n], "#", "{", [A-Za-z0-9_-], '"', "'", "}" at line 17, column 1 (byte 618) after input {spa

        報錯緣由:配置文件中17行有錯誤

        解決方法:檢查配置文件的內容,是否合規

啓動報錯記錄3:

[ERROR][logstash.inputs.jdbc     ] Invalid setting for jdbc input plugin:

  input {
    jdbc {
      # This setting must be a number
      # Expected number, got "" (type )
      jdbc_page_size => ""
      ...
    }
  }

        報錯緣由:jdbc_page_size必須是一個數字

        解決方法:jdbc_page_size => "10"

#驗證配置文件命令:
./logstash -f sync_tablename.conf --config.test_and_exit #顯示內容表示配置文件正確 Configuration OK [INFO ][logstash.runner ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash

 四、啓動命令進行數據同步   ./logstash -f sync_tablename.conf

啓動報錯記錄1:

Error: unable to load /home/u02/tomcat/logstash-7.6.2/bin/config-mysql/mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar from :jdbc_driver_library, file not readable (please check user and group permissions for the path)
  Exception: LogStash::PluginLoadingError

        報錯緣由:插件加載失敗,mysql-connector-java插件路徑配置錯誤,

        解決方法:去掉/home,改成    /u02/tomcat/logstash-7.6.2/bin/config-mysql/mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar

啓動報錯記錄2:

[WARN ][logstash.outputs.elasticsearch][main] Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>"%{id}", :_index=>"user", :routing=>nil, :_type=>"doc"}, #<LogStash::Event:0x4d04559>], :response=>{"index"=>{"_index"=>"user", "_type"=>"doc", "_id"=>"%{id}", "status"=>400, "error"=>{"type"=>"illegal_argument_exception", "reason"=>"Rejecting mapping update to [user] as the final mapping would have more than 1 type: [_doc, doc]"}}}}
{"@timestamp":"2020-07-02T10:11:23.182Z","message":"","host":"127.0.0.1","@version":"1"}

        報錯緣由:沒法將事件索引到Elasticsearch,拒絕對[user]的映射更新,由於最終映射將有一個以上的類型:[_doc,doc]」

        解決方法:配置文件中的type要和elasticSearch中的type保持一致

相關文章
相關標籤/搜索