通過logstash增量采集mysql的數(shù)據(jù)-創(chuàng)新互聯(lián)

下文給大家?guī)碛嘘P(guān)通過logstash增量采集mysql的數(shù)據(jù)內(nèi)容,相信大家一定看過類似的文章。我們給大家?guī)淼挠泻尾煌兀恳黄饋砜纯凑牟糠职?,相信看完通過logstash增量采集mysql的數(shù)據(jù)你一定會有所收獲。

創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元鞍山做網(wǎng)站,已為上家服務(wù),為鞍山各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

最近有一項需求需要把mysql中一個表中的數(shù)據(jù)同步到es中,分析后使用logstash的jdbc插件獲取mysql中的數(shù)據(jù),output到es中,采集的情況分兩種:開始是全量的采集,之后是增量采集。

已下式驗證的過程:

1.安裝logstash和mysql

mysql和logstash的安裝就不多說了,可以在網(wǎng)上自行查找。

需要注意的事項是:

    1)可能需要安裝:bin/plugin install logstash-input-jdbc

    2)mysql的driver下載https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz

2.創(chuàng)建mysql表和數(shù)據(jù)

create table test.zy  (
id int,
str varchar(20)
) ;

insert into test.zy   values('1','a1');
insert into test.zy   values('2','a2');
insert into test.zy   values('3','a3');
insert into test.zy   values('4','a4');
insert into test.zy   values('5','a5');
insert into test.zy   values('6','a6');
insert into test.zy   values('7','a7');
insert into test.zy   values('8','a8');
insert into test.zy   values('9','a9');
insert into test.zy   values('10','a10');
insert into test.zy   values('11','a11');
insert into test.zy   values('12','a12');
insert into test.zy   values('13','a13');
insert into test.zy   values('14','a14');
#增量采集驗證插入的數(shù)據(jù)
insert into test.zy   values('15','a15');
insert into test.zy   values('16','a16');

3.logstash采集mysql的配置文件

#根據(jù)字段增量采集
input {
    jdbc {
        jdbc_connection_string => "jdbc:mysql://ip:3306/test"
        jdbc_user => "root"
        jdbc_password => "123456"
        jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        statement => "select * from zy where id > :sql_last_value"
	use_column_value => true
	tracking_column => "id"
	record_last_run => true
	last_run_metadata_path => "/root/test.log"
	schedule => "*/2 * * * *"
     }
}

output {
  file {
   path => "./mysql/test-%{+YYYY-MM-dd}.txt"
  }
}

#根據(jù)時間戳增量采集
input {
    jdbc {
        jdbc_connection_string => "jdbc:mysql://ip:port/test"
        jdbc_user => "root"
        jdbc_password => "123456"
        jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_default_timezone =>"Asia/Shanghai"  #設(shè)置sql_last_value記錄的時間時區(qū),不然會影響增量采集的效果
        statement => "select * from zy1 where time > :sql_last_value"
        use_column_value => false
        record_last_run => true
        last_run_metadata_path => "/root/test.log"
        schedule => "*/2 * * * *"
     }
}

output {
  file {
   path => "./db2/test-%{+YYYY-MM-dd}.txt"
  }
}

input的配置解析
statement  執(zhí)行myqsl的語句,也可以是statementpath 后跟sql文件的路徑
use_column_value 
    是否使用列值作為依據(jù),進行上次運行位置的記錄。
    如果設(shè)置為true,則使用tracking_column定義的列,作為:sql_last_value.
    如果設(shè)置為false,則:sql_last_value反映的是上次SQL的運行時間。
tracking_column   增量采集依據(jù)的字段名   如果 use_colomn_value為false時,可以不寫
record_last_run   是否記錄本次采集數(shù)據(jù)的位置
last_run_metadata_path   設(shè)置記錄采集數(shù)據(jù)位置的文件
schedule         sql腳本執(zhí)行的頻率

4.采集驗證

  啟動logstash

    cd /usr/local/logstash

    bin/logstash -f conf/logtash.conf 

    ##logstash 自動加載配置文件  bin/logstash -f conf/logtash.conf  --config.reload.automatic

        測試logtash是否正常使用

        bin/logstash -e 'input { stdin{}} output { output{}}'

        測試并退出

        bin/logstash -f conf/logtash.conf  --config.test_and_exit

    logstash采集輸出日志:

   通過logstash增量采集mysql的數(shù)據(jù)

  

輸出文件內(nèi)容查看

通過logstash增量采集mysql的數(shù)據(jù)

sql_last_value的記錄

通過logstash增量采集mysql的數(shù)據(jù)

對于上文關(guān)于通過logstash增量采集mysql的數(shù)據(jù),大家覺得是自己想要的嗎?如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享標(biāo)題:通過logstash增量采集mysql的數(shù)據(jù)-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article26/cdejcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、Google、網(wǎng)站收錄、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)