当前位置:   article > 正文

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

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

 

  1. input {
  2. stdin {
  3. }
  4. jdbc {
  5. #serverTimezone=UTC
  6. #serverTimezone=Asia/Shanghai
  7. jdbc_connection_string => "jdbc:mysql://localhost:3306/xc_course?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8"
  8. # the user we wish to excute our statement as
  9. jdbc_user => "root"
  10. jdbc_password => "1234"
  11. # the path to our downloaded jdbc driver
  12. jdbc_driver_library => "../lib/mysql-connector-java-5.1.47.jar"
  13. # the name of the driver class for mysql
  14. jdbc_driver_class => "com.mysql.jdbc.Driver"
  15. #分页设置
  16. jdbc_paging_enabled => "true"
  17. jdbc_page_size => "50000"
  18. jdbc_fetch_size => 100
  19. #要执行的sql文件
  20. #statement_filepath => "/conf/course.sql"
  21. #statement => "select * from course_pub where timestamp > date_add(:sql_last_value,INTERVAL 8 HOUR)"
  22. statement => "select * from course_pub where timestamp > :sql_last_value"
  23. #定时配置
  24. schedule => "* * * * *"
  25. record_last_run => true
  26. last_run_metadata_path => "../config/logstash_metadata"
  27. }
  28. }
  29. output {
  30. elasticsearch {
  31. #ES的ip地址和端口
  32. hosts => "localhost:9200"
  33. #hosts => ["localhost:9200","localhost:9202","localhost:9203"]
  34. #ES索引库名称
  35. index => "xc_course"
  36. document_id => "%{id}"
  37. document_type => "CoursePub"
  38. template =>"../config/xc_course_template.json"
  39. template_name =>"xc_course"
  40. template_overwrite =>"true"
  41. }
  42. stdout {
  43. #日志输出
  44. codec => json_lines
  45. }
  46. }

 

 

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

  1. {
  2. "template" : "xc_course",
  3. "mappings" : {
  4. "CoursePub": {
  5. "properties": {
  6. "charge": {
  7. "type": "keyword"
  8. },
  9. "description": {
  10. "type": "text",
  11. "analyzer": "ik_max_word",
  12. "search_analyzer": "ik_smart"
  13. },
  14. "end_time": {
  15. "type": "date",
  16. "format": "yyyy-MM-dd HH:mm:ss"
  17. },
  18. "expires": {
  19. "type": "date",
  20. "format": "yyyy-MM-dd HH:mm:ss"
  21. },
  22. "grade": {
  23. "type": "keyword"
  24. },
  25. "id": {
  26. "type": "keyword"
  27. },
  28. "mt": {
  29. "type": "keyword"
  30. },
  31. "name": {
  32. "type": "text",
  33. "analyzer": "ik_max_word",
  34. "search_analyzer": "ik_smart"
  35. },
  36. "pic": {
  37. "type": "keyword",
  38. "index": false
  39. },
  40. "price": {
  41. "type": "float"
  42. },
  43. "price_old": {
  44. "type": "float"
  45. },
  46. "pub_time": {
  47. "type": "date",
  48. "format": "yyyy-MM-dd HH:mm:ss"
  49. },
  50. "qq": {
  51. "type": "keyword",
  52. "index": false
  53. },
  54. "st": {
  55. "type": "keyword"
  56. },
  57. "start_time": {
  58. "type": "date",
  59. "format": "yyyy-MM-dd HH:mm:ss"
  60. },
  61. "status": {
  62. "type": "keyword"
  63. },
  64. "studymodel": {
  65. "type": "keyword"
  66. },
  67. "teachmode": {
  68. "type": "keyword"
  69. },
  70. "teachplan": {
  71. "type": "text",
  72. "analyzer": "ik_max_word",
  73. "search_analyzer": "ik_smart"
  74. },
  75. "users": {
  76. "type": "text",
  77. "index": false
  78. },
  79. "valid": {
  80. "type": "keyword"
  81. }
  82. }
  83. }
  84. }
  85. }

 

 

  • 步骤四:启动logstash

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

 

  • 启动成功:

 

  • 访问:

 

 

 

 

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/312675
推荐阅读
相关标签
  

闽ICP备14008679号