elasticsearch 使用 Logstash 做数据采集

2024-08-22 16:08

本文主要是介绍elasticsearch 使用 Logstash 做数据采集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1,下载
下载地址(根据自己需要的版本下载):
https://www.elastic.co/cn/downloads/logstash

我这里是使用的6.2.1版本,直接下载就可以了

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.1.tar.gz

2,解压

tar -zxvf logstash-6.2.1.tar.gz

将解压后的目录移动到/usr/local/目录下

mv logstash-6.2.1 /usr/local/
cd /usr/local/logstash-6.2.1/

3,安装 logstash 所需依赖 ruby 和 rubygems(注意:需要 ruby 的版本在 1.8.7 以上)

yum install -y ruby rubygems

检查 ruby 版本

ruby -v

输出如下,表示安装成功
在这里插入图片描述
4,安装 logstash-input-jdbc

cd /usr/local/logstash-6.2.1/
./bin/logstash-plugin install --no-verify  logstash-input-jdbc

5,编写配置文件
我这里的配置文件主要是2个配置文件,mysql同步表文件(mysql.conf)和索引库映射文件(question_template.json),都放在 logstash 的 config 配置文件下
1,mysql.conf

input {stdin {}jdbc {jdbc_connection_string => "jdbc:mysql://192.168.1.1:3306/java_interview_dev?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai"# the user we wish to excute our statement asjdbc_user => "root"jdbc_password => "123456"# the path to our downloaded jdbc driverjdbc_driver_library => "/usr/local/logstash-6.2.1/lib/mysql-connector-java-8.0.16.jar"# the name of the driver class for mysqljdbc_driver_class => "com.mysql.cj.jdbc.Driver"jdbc_paging_enabled => "true"jdbc_page_size => "50000"#要执行的sql文件#statement_filepath => "/conf/course.sql"statement => "SELECT question_id, title, answer, type_ids, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') AS create_time FROM question WHERE `timestamp` > DATE_ADD(:sql_last_value,INTERVAL 8 HOUR)"#定时配置schedule => "* * * * *"record_last_run => true#记录最后采集时间点,保存到logstash_metadata文件中last_run_metadata_path => "/usr/local/logstash-6.2.1/config/logstash_metadata"}
}output {elasticsearch {#ES的ip地址和端口hosts => "localhost:9200"#hosts => ["localhost:9200"]#ES索引库名称index => "question_dev"document_id => "%{question_id}"document_type => "doc"template =>"/usr/local/logstash-6.2.1/config/question_template.json"template_name =>"question_dev"template_overwrite =>"true"}stdout {#日志输出codec => json_lines}
}

2,question_template.json

{"mappings": {"doc": {"properties": {"question_id": {"type": "integer"},"title": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"answer": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"type_ids": {"type": "text"},"create_time": {"format": "yyyy-MM-dd HH:mm:ss","type": "date"}}}},"template": "question_dev"
}

6,运行

/usr/local/logstash-6.2.1/bin/logstash -f /usr/local/logstash-6.2.1/config/mysql.conf

使 logstash 一直保持在后台运行命令:

nohup /usr/local/logstash-6.2.1/bin/logstash -f /usr/local/logstash-6.2.1/config/mysql.conf 2>&1 &

运行前:
在这里插入图片描述
索引库里面没有一条数据,
运行后:
在这里插入图片描述
运行后我们发现,logstash 会根据 mysql.conf 里面的配置项 statement 执行的sql所查询到的数据全部录入到索引库,默认的 logstash 会每分钟执行一次,可以根据配置的 schedule 定时任务修改

到这里使用 logstash 做es数据采集的过程就已经全部完成了

备注:配置不太明白的可以看我附件上传的教学视频

https://download.csdn.net/download/u012946310/11827678

备注:如果需要同时对多个数据采集并且输出到不同的索引库,参考如下配置:

input {stdin {}#dev数据库问题索引jdbc {#采集类型,避免输出时混淆,使用此类型判断输出type => "dev_question"jdbc_connection_string => "jdbc:mysql://localhost:3306/cx_blockchain_dev?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai"# the user we wish to excute our statement asjdbc_user => "root"jdbc_password => "cx123456789cx"# the path to our downloaded jdbc driverjdbc_driver_library => "/usr/local/logstash-6.2.1/lib/mysql-connector-java-8.0.16.jar"# the name of the driver class for mysqljdbc_driver_class => "com.mysql.cj.jdbc.Driver"jdbc_paging_enabled => "true"jdbc_page_size => "50000"#要执行的sql文件#statement_filepath => "/conf/course.sql"statement => "SELECT question_id, title, `desc`, label_code, answer_count, create_user_id, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') AS create_time FROM question WHERE `timestamp` > DATE_ADD(:sql_last_value,INTERVAL 8 HOUR)"#定时配置#schedule => "*/10 * * * *"schedule => "* * * * *"record_last_run => true#记录最后采集时间点,保存到dev_question_run_log文件中last_run_metadata_path => "/usr/local/logstash-6.2.1/config/es-conf/dev_question_run_log"}#test数据库问题索引jdbc {#采集类型,避免输出时混淆,使用此类型判断输出type => "test_question"jdbc_connection_string => "jdbc:mysql://localhost:3306/cx_blockchain_test?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai"# the user we wish to excute our statement asjdbc_user => "root"jdbc_password => "cx123456789cx"# the path to our downloaded jdbc driverjdbc_driver_library => "/usr/local/logstash-6.2.1/lib/mysql-connector-java-8.0.16.jar"# the name of the driver class for mysqljdbc_driver_class => "com.mysql.cj.jdbc.Driver"jdbc_paging_enabled => "true"jdbc_page_size => "50000"#要执行的sql文件#statement_filepath => "/conf/course.sql"statement => "SELECT question_id, title, `desc`, label_code, answer_count, create_user_id, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') AS create_time FROM question WHERE `timestamp` > DATE_ADD(:sql_last_value,INTERVAL 8 HOUR)"#定时配置#schedule => "*/12 * * * *"schedule => "* * * * *"record_last_run => true#记录最后采集时间点,保存到test_question_run_log文件中last_run_metadata_path => "/usr/local/logstash-6.2.1/config/es-conf/test_question_run_log"}
}output {#dev_question索引输出if[type]=="dev_question"{elasticsearch {#ES的ip地址和端口hosts => "localhost:9200"#hosts => ["localhost:9200"]#ES索引库名称index => "dev_question"document_id => "%{question_id}"document_type => "doc"template =>"/usr/local/logstash-6.2.1/config/es-conf/question_template.json"template_name =>"question"template_overwrite =>"true"}stdout {#日志输出codec => json_lines}}#test_question索引输出if[type]=="test_question"{elasticsearch {#ES的ip地址和端口hosts => "localhost:9200"#hosts => ["localhost:9200"]#ES索引库名称index => "test_question"document_id => "%{question_id}"document_type => "doc"template =>"/usr/local/logstash-6.2.1/config/es-conf/question_template.json"template_name =>"question"template_overwrite =>"true"}stdout {#日志输出codec => json_lines}}
}

上面主要新增了一个 type 字段,并且在输出的时候判断 type 字段,以此来区分采集的数据输出到不同的索引库

这篇关于elasticsearch 使用 Logstash 做数据采集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1096745

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1