使用 Logstash 进行数据摄取
学习使用 Logstash 从各种来源收集、解析和转换数据,然后将其索引到 Elasticsearch 中。
使用 Logstash 进行数据摄取 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Intro to Logstash
Welcome to Logstash! It's a powerful, open-source data collection engine with real-time pipelining capabilities. It's a key component of the Elastic Stack (ELK stack), alongside Elasticsearch and Kibana.
Think of Logstash as the 'L' in ELK. Its job is to ingest data from various sources, process it, and then send it to a 'stash' (often Elasticsearch) for storage and analysis.
The Logstash Pipeline
Logstash works by processing data through a pipeline. This pipeline consists of three main stages:
- Input: Where data is collected from its source.
- Filter: Where data is processed, parsed, and transformed.
- Output: Where processed data is sent to its destination.
Data flows sequentially from input to filter to output, allowing for flexible and powerful data manipulation.
Input Stage: Collecting Data
The input stage is responsible for collecting data from various sources. Logstash supports a wide array of input plugins, allowing it to connect to almost any data source.
Common input sources include:
- Files: Reading logs from disk.
- Beats: Receiving data from lightweight data shippers like Filebeat or Metricbeat.
- HTTP/TCP/UDP: Listening for network traffic.
- Databases: Pulling data from relational databases.
Input Example: Reading Files
Here's a simple Logstash configuration snippet using the file input plugin. This tells Logstash to read all .log files from the specified directory.
The type field helps categorize the incoming events, which can be useful later in filters or outputs.
input {
file {
path => "/var/log/*.log"
type => "syslog"
start_position => "beginning"
}
}Filter Stage: Transforming Data
The filter stage is where the magic happens! This is where you parse, modify, and enrich your raw data before it's sent to its destination.
Filter plugins can:
- Parse unstructured data: Like Apache logs using Grok.
- Mutate fields: Rename, remove, or add new fields.
- Add geographic data: Based on IP addresses using GeoIP.
- Perform conditional logic: Process data differently based on its content.
Filter Example: Grok Parser
The grok filter is incredibly powerful for parsing unstructured log data into structured fields. It uses regular expressions but with pre-defined patterns for common log formats.
This example uses the COMBINEDAPACHELOG pattern to parse a typical Apache web server log line, extracting fields like IP address, timestamp, request, and status code.
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
}Output Stage: Sending Data
Finally, the output stage is where Logstash sends the processed events. An event can be sent to multiple outputs simultaneously.
Common output destinations include:
- Elasticsearch: The most common destination for further indexing and search.
- Stdout: For debugging and testing your pipeline.
- File: Writing processed data to a new file.
- Kafka/Redis: For queuing or further processing by other systems.
Output Example: To Elasticsearch
This is a standard output configuration to send your processed data to an Elasticsearch cluster. You specify the hosts (your Elasticsearch node addresses) and the index name.
The %{+YYYY.MM.dd} syntax dynamically creates daily indices, which is a common practice for time-series data.
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "my-logs-%{+YYYY.MM.dd}"
}
}Building a Full Pipeline
Now, let's combine all three stages into a single, complete Logstash configuration file. This pipeline reads Nginx access logs, parses them with Grok, and then sends the structured data to Elasticsearch.
This configuration would typically be saved as a .conf file, e.g., nginx-pipeline.conf.
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
}
}
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "nginx-access-%{+YYYY.MM.dd}"
}
}Running Logstash
To run your Logstash pipeline, you typically execute the logstash command-line tool, pointing it to your configuration file.
Before running it for real, it's good practice to test your configuration file for syntax errors using the --config.test_and_exit flag. This ensures your pipeline is valid before processing any data.
bin/logstash -f nginx-pipeline.conf --config.test_and_exit
# To run the pipeline
bin/logstash -f nginx-pipeline.confQuick Check
Which of the following statements about Logstash's pipeline stages are TRUE?
Logstash in Review
In this lesson, we explored Logstash, a crucial part of the Elastic Stack for data ingestion. We learned about its core pipeline concept, comprising input, filter, and output stages.
You now understand how Logstash collects data from various sources, transforms it using powerful filters like Grok, and then dispatches it to destinations such as Elasticsearch. This capability is essential for preparing diverse data for effective search and analysis.
常见问题解答
「使用 Logstash 进行数据摄取」课时是免费的吗?
是的 — 「使用 Logstash 进行数据摄取」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「使用 Logstash 进行数据摄取」这节课中我会学到什么?
学习使用 Logstash 从各种来源收集、解析和转换数据,然后将其索引到 Elasticsearch 中。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Logstash 进行数据摄取」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 Kibana 进行可视化
- 使用 Logstash 进行数据摄取
- 与应用程序集成(客户端)
- 用于轻量级数据传输的 Beats