使用 logstash-integration-jdbc 同步数据库中的数据(全量 和 增量)

一、安装

logstash-integration-jdbc 是logstash 的一个插件,可以通过logstash的将mysql中的数据直接同步到es中,从而在kibana中展示。 logstash-integration-jdbc 之前的名称叫 logstash-input-jdbc .
1、替换国内镜像

gem source --remove https://rubygems.org/
gem source --add https://gems.ruby-china.com/

备注:logstash-integration-jdbc是使用ruby语言开发的,因此需安装gem,gem是Ruby的一个包管理器

2、修改Gemfile数据源地址
①进入logstash的安装目录,修改Gemfile文件 ,将Gemfile的source换成https://gems.ruby-china.com/
②修改Gemfile.lock文件,将GEM remote修改为https://gems.ruby-china.com/

3、安装 logstash-integration-jdbc 插件

##进入bin目录
cd /opt/logstash/bin/
sudo ./logstash-plugin install logstash-integration-jdbc

# 显示如下信息即安装成功
Validating logstash-integration-jdbc
Installing logstash-integration-jdbc
Installation successful

二、数据全量同步

全量同步是指全部将数据同步到es,通常是刚建立es,第一次同步时使用。
编写 jdbc.conf 文件,内容如下

input {
  jdbc {
    # 驱动
    jdbc_driver_library => "/opt/logstash/lib/mysql-connector-java/mysql-connector-java-5.1.49-bin.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
	# 数据库连接信息
    jdbc_connection_string => "jdbc:mysql://localhost:3306/report"
    jdbc_user => "root"
    jdbc_password => "root"
	# 设置监听间隔  各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新
    schedule => "* * * * *"
	# 执行的sql
    statement => "select id,shop_id,profits,record_month from month_report"
	# 索引类型
    type => "jdbc"
  }
}

filter {
    json {
        source => "message"
        remove_field => ["message"]
    }
}

output {
    elasticsearch {
        hosts => ["127.0.0.1:9200"]
        index => "report"
		# 自增ID 需要关联的数据库中有有一个id字段,对应索引的id号
        document_id => "%{id}"
        document_type => "_doc"
    }
    stdout {
	    # JSON格式输出
        codec => json_lines
    }
}

扩展:

input中的 statement 可以用  statement_filepath 代替,statement_filepath 书写格式如下
 # 执行的sql 文件路径+名称
statement_filepath => "/opt/logstash/config/jdbc.sql"


## jdbc.sql 文件内容如下
cat >  /opt/logstash/config/jdbc.sql << END
select id,shop_id,profits,record_month from month_report
END

三、数据增量同步

增量同步是指将后续的更新、插入记录同步到es。

input {
  jdbc {
    jdbc_driver_library => "/opt/logstash/lib/mysql-connector-java/mysql-connector-java-5.1.49-bin.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_connection_string => "jdbc:mysql://localhost:3306/report"
    jdbc_user => "root"
    jdbc_password => "root"
    schedule => "* * * * *"
    statement => "select id,shop_id,profits,record_month from month_report where es_update_time > :sql_last_value"
    type => "jdbc"

    #处理中文乱码问题
    codec => plain { charset => "UTF-8"}
    #使用其它字段追踪,而不是用时间
    use_column_value => true
    #追踪的字段
    tracking_column => id
    record_last_run => true
    #上一个sql_last_value值的存放文件路径, 必须要在文件中指定字段的初始值
    last_run_metadata_path => "/opt/logstash/config/station_parameter.txt"
    #开启分页查询
    jdbc_paging_enabled => true
    jdbc_page_size => 300
  }
}


filter {
  json {
    source => "message"
    remove_field => ["message"]
  }
}

output {
  elasticsearch {
    hosts => ["127.0.0.1:9200"]
    index => "report"
    document_id => "%{id}"
    document_type => "_doc"
  }
  stdout {
    codec => json_lines
  }
}

参数说明:

//是否记录上次执行结果, 如果为真,将会把上次执行到的 tracking_column 字段的值记录下来,保存到 last_run_metadata_path 指定的文件中
record_last_run => true

//是否需要记录某个column 的值,如果 record_last_run 为真,可以自定义我们需要 track 的 column 名称,此时该参数就要为 true. 否则默认 track 的是 timestamp 的值.
use_column_value => true

//如果 use_column_value 为真,需配置此参数. track 的数据库 column 名,该 column 必须是递增的.比如:ID.
tracking_column => MY_ID

//指定文件,来记录上次执行到的 tracking_column 字段的值
//比如上次数据库有 10000 条记录,查询完后该文件中就会有数字 10000 这样的记录,下次执行 SQL 查询可以从 10001 条处开始.
//我们只需要在 SQL 语句中 WHERE MY_ID > :last_sql_value 即可. 其中 :last_sql_value 取得就是该文件中的值(10000).
last_run_metadata_path => "/opt/logstash/config/station_parameter.txt"


//是否清除 last_run_metadata_path 的记录,如果为真那么每次都相当于从头开始查询所有的数据库记录
clean_run => false

//是否将 column 名称转小写
lowercase_column_names => false

四、运行时可能会报错Pipelines YAML文件为空

在config目录下修改pipelines.yml,将一下内容的注释去掉

- pipeline.id: test
  pipeline.workers: 1
  pipeline.batch.size: 1
  config.string: "input { generator {} } filter { sleep { time => 1 } } out    put { stdout { codec => dots } }"
- pipeline.id: another_test
  queue.type: persisted
  path.config: "/opt/logstash/config/*.config"

重新跑自己写好的文件即可

nohup /opt/logstash/bin/logstash -r &
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值