logstash 同步 mysql 多张表到 es

学习笔记 马富天 2020-02-12 14:39:27 41 0

【摘要】上一篇文章讲了 logstash 自动同步单张数据表到 es 中,本文将同步至多张表,多张表的基本步骤是和单张表是一样的,区别在于配置文件的配置不同。

接下来我们给出同步两张表的小例子,数据准备还是参考上一篇文章中的数据:

/438.html

logstash.conf 配置文件:

  1. input {
  2.     stdin {
  3.     
  4.     }
  5.     # jdbc-1.sql 的 jdbc 配置
  6.     jdbc {
  7.         jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/jy?useUnicode=true&characterEncoding=utf-8&useSSL=false"
  8.         jdbc_user => "root"
  9.         jdbc_password => "root"
  10.         jdbc_driver_library => "bin/mysql-connector-java-5.1.7-bin.jar"
  11.         jdbc_driver_class => "com.mysql.jdbc.Driver"
  12.         jdbc_paging_enabled => "true"
  13.         jdbc_page_size => "50000"
  14.         jdbc_default_timezone =>"Asia/Shanghai"
  15.         statement => "select * from student"
  16.         # 也可以在对应的文件中写 sql 
  17.         # statement_filepath => "jdbc.sql"
  18.         
  19.         schedule => "* * * * *"
  20.         lowercase_column_names => false
  21.         # type 用来区分不同的同步任务
  22.         type => "jdbc_student"
  23.     }
  24.     # jdbc-2.sql 的 jdbc 配置
  25.     jdbc {
  26.         jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/jy?useUnicode=true&characterEncoding=utf-8&useSSL=false"
  27.         jdbc_user => "root"
  28.         jdbc_password => "root"
  29.         jdbc_driver_library => "bin/mysql-connector-java-5.1.7-bin.jar"
  30.         jdbc_driver_class => "com.mysql.jdbc.Driver"
  31.         jdbc_paging_enabled => "true"
  32.         jdbc_page_size => "50000"
  33.         jdbc_default_timezone =>"Asia/Shanghai"
  34.         statement => "select * from class"
  35.         schedule => "* * * * *"
  36.         lowercase_column_names => false
  37.         type => "jdbc_class"
  38.     }
  39. }
  40. output {
  41.     # 上面 type = "jdbc_student" 对应的同步任务 
  42.     if[type] == "jdbc_student"{
  43.         elasticsearch {
  44.             hosts => ["localhost:9200"]
  45.             # 索引名称
  46.             index => "index_student"
  47.             document_id => "%{id}"
  48.             document_type => "test"
  49.         }
  50.     }
  51.     if[type] == "jdbc_class"{
  52.         elasticsearch {
  53.             hosts => ["localhost:9200"]
  54.             index => "index_class"
  55.             document_id => "%{id}"
  56.             document_type => "test"
  57.         }
  58.     }
  59.     stdout {
  60.         codec => json_lines
  61.     }
  62. }
请输入图片名称

如图所示,已经同步成功啦!

版权归 马富天个人博客 所有

本文标题:《logstash 同步 mysql 多张表到 es》

本文链接地址:http://www.mafutian.com/439.html

转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^

0

0

上一篇《 使用 Logstash 将 MySQL 数据同步到 Elasticsearch(版本6.5.1) 》 下一篇《 linux 下 Python 脚本后台执行(nohup command &) 》

暂无评论

评论审核未开启
表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情
验证码

TOP10

  • 浏览最多
  • 评论最多