1、下載官方Logstash
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz
2、配置logstash-input-jdbc插件環境
#查看gem環境
gem
RubyGems is a sophisticated package manager for Ruby. This is a
basic help message containing pointers to more information.
Usage:
gem -h/--help
gem -v/--version
gem command [arguments...] [options...]
Examples:
gem install rake
gem list --local
gem build package.gemspec
gem help install
Further help:
gem help commands list all 'gem' commands
gem help examples show some examples of usage
gem help platforms show information about platforms
gem help <COMMAND> show help on COMMAND
(e.g. 'gem help install')
gem server present a web page at
http://localhost:8808/
with info about installed gems
Further information:
http://guides.rubygems.org
顯示出gem幫助信息,說明已經安裝。
3、如果沒有安裝gem
yum install gem -y
4、gem源修改
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
5、查看gem源
gem sources -l
6、修改logstash目錄中的Gemfile文件
vim Gemfile
source "https://gems.ruby-china.org/"
7、修改logstash目錄中的Gemfile.lock文件
vim Gemfile.lock
remote https://gems.ruby-china.org/
8、安裝gem bundler
gem install bundler
9、安裝logstash-input-jdbc插件
bin/logstash-plugin install logstash-input-jdbc
10、上傳數據庫驅動文件mysql-connector-java-5.1.44.jar到logstash目錄。
11、編寫logstash-input-jdbc-mysql.conf文件
input {
jdbc {
// mysql相關jdbc配置
jdbc_connection_string => "jdbc:mysql://localhost:3306/pgeniusdb_new"
jdbc_user => "root"
jdbc_password => "123456"
//mysql驅動,位置寫絕對路徑
jdbc_driver_library => "/data/app/logstash-6.3.2/mysql/mysql-connector-java-5.1.46.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
//sql可以寫在這裏也可以寫在文件裏,位置絕對路徑
statement_filepath => "/data/app/logstash-6.3.2/mysql/jdbc-news.sql"
// 這裏類似crontab,可以定製定時操作,比如每10分鐘執行一次同步(分 時 天 月 年
schedule => "*/10 * * * *"
// 是否需要記錄某個column 的值,如果record_last_run爲真,可以自定義我們需要 track 的 column 名稱,此時該參數就要爲 true. 否則默認 track 的是 timestamp 的值.
use_column_value => "true"
// 如果 use_column_value 爲真,需配置此參數. track 的數據庫 column 名,該 column 必須是遞增的. 一般是mysql主鍵
tracking_column => "objectId"
//分類
type => "news"
// 是否清除 last_run_metadata_path 的記錄,如果爲真那麼每次都相當於從頭開始查詢所有的數據庫記錄
clean_run => "false"
//是否將 字段(column) 名稱轉小寫
lowercase_column_names => "false"
}
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/pgeniusdb_new"
jdbc_user => "root"
jdbc_password => "123456"
jdbc_driver_library => "/data/app/logstash-6.3.2/mysql/mysql-connector-java-5.1.46.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
statement_filepath => "/data/app/logstash-6.3.2/mysql/jdbc-news-industry.sql"
schedule => "*/10 * * * *"
use_column_value => "true"
tracking_column => "objectId"
type => "news_industry"
}
}
//filter沒什麼用
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
if[type] == "news" {
elasticsearch {
//是http地址,不是tcp,這個是個坑
hosts => ["localhost:8607"]
//索引名字
index => "index_news"
document_id => "%{objectid}"
//類型名字
document_type => "news"
}
}
if[type] == "news_industry" {
elasticsearch {
hosts => ["localhost:8607"]
index => "index_news"
document_id => "%{objectid}"
document_type => "news"
}
}
stdout {
codec => json_lines
}
}
在output中,我們沒有傳document_type,並且還使用了兩個index。這是因爲在elasticsearch6.0中,一個索引下只能有一個類型,不然會報錯。這裏我們可
12、執行同步
/bin/logstash -f logstash-input-jdbc-mysql.conf
踩過的坑
1、配置文件中,當在input的jdbc下,增加type屬性時,會導致該索引下增加type字段。所以sql查詢出的字段不要用type,如果有,as成其他的名字,不然的話,這裏判斷會有異常
2、同步多個表,elasticsearch6.0以上的版本,一定要設置多個索引
歡迎關注小編微信公衆號:程序猿微刊 ,有更多的幹活和資源等你來拿