Logstash 技术实现数据同步快速入门

 

Logstash 数据同步

一.概述

Logstash是一个开源的服务器端数据处理管道,可以同时从多个数据源获取数据,并对其进行转换,然后将其发送到Elasticsearch中。

 

  • Logstash 采用可插拔框架,拥有 200 多个插件。您可以将不同的输入选择、过滤器和输出选择混合搭配、精心安排,让它们在管道中和谐地运行。

 

二.​​​​​​​下载

https://www.elastic.co/cn/downloads/past-releases#logstash

  • 可自行选择版本下载

 

 

 

三.​​​​​​​安装

解压即可

 

 

四.​​​​​​​数据同步

Logstash的工作是从MySQL中读取数据,向ES中创建索引,这里需要提前创建mapping的模板文件以便logstash使用。

 

  • 步骤一:拷贝数据库驱动

 

 

  • 步骤二:创建  config/xc_course_mysql.conf ,用于描述从哪个数据库读取数据

  • 创建的文件必须是UTF-8编码,且无BOM

 

input {
  stdin {
  }
  jdbc {
	#serverTimezone=UTC
	#serverTimezone=Asia/Shanghai
    jdbc_connection_string => "jdbc:mysql://localhost:3306/xc_course?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8"
    # the user we wish to excute our statement as
    jdbc_user => "root"
    jdbc_password => "1234"
    # the path to our downloaded jdbc driver  
    jdbc_driver_library => "../lib/mysql-connector-java-5.1.47.jar"
    # the name of the driver class for mysql
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    #分页设置
    jdbc_paging_enabled => "true"
    jdbc_page_size => "50000"
    jdbc_fetch_size => 100
    #要执行的sql文件
    #statement_filepath => "/conf/course.sql"
    #statement => "select * from course_pub where timestamp > date_add(:sql_last_value,INTERVAL 8 HOUR)"
    statement => "select * from course_pub where timestamp > :sql_last_value"
    #定时配置
    schedule => "* * * * *"
    record_last_run => true
    last_run_metadata_path => "../config/logstash_metadata"
  }
}


output {
  elasticsearch {
    #ES的ip地址和端口
    hosts => "localhost:9200"
    #hosts => ["localhost:9200","localhost:9202","localhost:9203"]
    #ES索引库名称
    index => "xc_course"
    document_id => "%{id}"
    document_type => "CoursePub"
    template =>"../config/xc_course_template.json"
    template_name =>"xc_course"
    template_overwrite =>"true"
  }
  stdout {
   #日志输出
    codec => json_lines
  }
}

 

 

  • 步骤三:创建 config/xc_course_template.json 用于配置logstash向elasticsearch写入数据的模板

{
	"template" : "xc_course",
	"mappings" : {
		"CoursePub": {
			"properties": {
			  "charge": {
				"type": "keyword"
			  },
			  "description": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_smart"
			  },
			  "end_time": {
				"type": "date",
				"format": "yyyy-MM-dd HH:mm:ss"
			  },
			  "expires": {
				"type": "date",
				"format": "yyyy-MM-dd HH:mm:ss"
			  },
			  "grade": {
				"type": "keyword"
			  },
			  "id": {
				"type": "keyword"
			  },
			  "mt": {
				"type": "keyword"
			  },
			  "name": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_smart"
			  },
			  "pic": {
				"type": "keyword",
				"index": false
			  },
			  "price": {
				"type": "float"
			  },
			  "price_old": {
				"type": "float"
			  },
			  "pub_time": {
				"type": "date",
				"format": "yyyy-MM-dd HH:mm:ss"
			  },
			  "qq": {
				"type": "keyword",
				"index": false
			  },
			  "st": {
				"type": "keyword"
			  },
			  "start_time": {
				"type": "date",
				"format": "yyyy-MM-dd HH:mm:ss"
			  },
			  "status": {
				"type": "keyword"
			  },
			  "studymodel": {
				"type": "keyword"
			  },
			  "teachmode": {
				"type": "keyword"
			  },
			  "teachplan": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_smart"
			  },
			  "users": {
				"type": "text",
				"index": false
			  },
			  "valid": {
				"type": "keyword"
			  }
			}
		}
	}
}

 

 

  • 步骤四:启动logstash

logstash.bat -f ..\config\xc_course_mysql.conf

 

  • 启动成功:

 

  • 访问:

 

 

 

 

看完恭喜你,又知道了一点点!!!

你知道的越多,不知道的越多! 

~感谢志同道合的你阅读,  你的支持是我学习的最大动力 ! 加油 ,陌生人一起努力,共勉!!

注: 如果本篇有需要改进的地方或错误,欢迎大神们指定一二~~

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值