Logstash实时同步mysql数据库至elastic

 

  1. 安装logstash-jdbc-input 插件(教程自己找)
  2. 运行 logstash -f D:\logstash-5.2.2\config\shipper.config
  3. shipper文件中的主要目标,定时刷新最新有变更的(依据update_date)数据至elastic,初始时直接插入,elastic检测到id相同时,则更新自有的数据。
  4. shipper.config 文件内容:
input {
    jdbc{
	 type => "sys_user"
	 #The path to our download jdbc driver
	 jdbc_connection_string => "jdbc:mysql://192.168.133.4:3306/orderproduction_temp?useUnicode=true&characterEncoding=utf-8"
	 jdbc_driver_library => "D:\logstash-5.2.2\driver\mysql-connector-java-5.1.39.jar"
	 jdbc_driver_class => "com.mysql.jdbc.driver"
	 
	 #The user we wish to execute our statement as
	 jdbc_user => "root"
	 jdbc_password => "dbserver666"
	 
	 #last_run_metadata_path => "./logstash_last_run_display"
	 
	 #every 5 minutes execute
	 #schedule => "*/5 * * * *"
	 schedule => "* * * * *"
	 #if clean_run set to true, sql_last_value is set to 19700101
	 #clean_run => true 
	 last_run_metadata_path => "./logstash_jdbc_last_run"
	 #out query
	 #parameters => {"name" => "长沙大东家"}
	 #statement => "select * from sys_user where name = :name"
	 statement => "select * from sys_user where update_date > :sql_last_value"
	 use_column_value => true
	 tracking_column => "update_date"
	 
	 jdbc_paging_enabled => "true"
	 jdbc_page_size => "1000"
	 
	 jdbc_default_timezone =>"Asia/Shanghai"
	}
}

filter {
			#grok{
			                #break on first match ,when there are multiple pattern need to cope
						    #break_on_match=>true
							#extract year and date info from create_date field
							#match => ["create_date","%{YEAR:year}-%{MONTH:month}-dd HH:mm:ss"]
							#add_field =>{
							#	"yearmonth" => "%{year}-%{month}"
							#}
			#}
			ruby {
		          #note 1 ruby need to be put before mutate
			      #note 2 if not run or correctly run ruby ,please install jruby and try it again
			      #in  linux, if hostname is no configured in hosts file, logstash get 0.0.0.0 ,so we add this code to get hostname on linux platform IO.popen('hostname')
			          #init => "require 'time'"
		              code => "
                               # IO.popen('hostname') { |io| while (line = io.gets) do event['host']=line end }
								# get date from path
								event.set('yearmonth',event.get('create_date').to_s[0..6])
                           "
		   }
			#date {
				 #use timezone is to avoid the value subtract timezone,if +8 zone,hours will be -8,in deploy situation ,please check the     hour value is correct, if not try remove timezone
			#	 timezone=>"Asia/Shanghai"
			#	 match => ["create_date","YYYY-MM-dd HH:mm:ss +0800"]
			#	 target => "@timestamp"
			#}
}

output {
   #stdout{ codec => json_lines  }
   stdout { codec => rubydebug }
   elasticsearch{
   "hosts" => "192.168.133.4:9200"
   "index" => "orderproduction_%{yearmonth}"
   "document_type" => "%{type}"
   "document_id" => "%{id}"
   doc_as_upsert => true
   action => "update"
   }
}

 

转载于:https://my.oschina.net/xiaohelong/blog/895829

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要实现LogstashMySQL实时同步,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了LogstashMySQL,并配置好相应的连接信息。 2. 在Logstash中使用jdbc输入插件,该插件可以从MySQL数据库读取数据。您可以在Logstash配置文件中添加以下内容: ```bash input { jdbc { jdbc_connection_string => "jdbc:mysql://localhost:3306/your_database" jdbc_user => "your_username" jdbc_password => "your_password" jdbc_driver_library => "/path/to/mysql-connector-java.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" statement => "SELECT * FROM your_table WHERE updated_at > :sql_last_value" use_column_value => true tracking_column => "updated_at" tracking_column_type => "timestamp" } } ``` 确保将上述配置中的数据库连接信息、表名和更新时间字段替换为您自己的信息。 3. 配置Logstash的输出插件,将数据发送到您希望同步的目的地。例如,您可以使用elasticsearch插件将数据发送到Elasticsearch: ```bash output { elasticsearch { hosts => ["localhost:9200"] index => "your_index" document_id => "%{id}" } } ``` 将上述配置中的Elasticsearch主机和索引名称替换为您自己的信息。 4. 启动Logstash,并它将会开始监视MySQL数据库,并将更改的数据实时同步Elasticsearch(或其他目的地)中。 请注意,这只是一个简单的示例配置,并且您可能需要根据自己的需求进行修改。同时,确保您已经安装了适当版本的MySQL JDBC驱动程序,并将`jdbc_driver_library`配置为正确的路径。 希望这些信息能对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值