快速开始
本指南逐步讲解如何在 GreptimeDB 中快速写入和查询日志。
GreptimeDB 支持可以将结构化日志消息解析并转换为多列的 Pipeline 机制, 以实现高效的存储和查询。
对于非结构化的日志,你可以不使用 Pipeline,直接将日志写入表。
使用 Pipeline 写入日志
使用 pipeline 可以自动将日志消息格式化并转换为多个列,并自动创建和修改表结构。
使用内置 Pipeline 写入 JSON 日志
GreptimeDB 提供了一个内置 pipeline greptime_identity 用于处理 JSON 日志格式。该 pipeline 简化了写入 JSON 日志的过程。
curl -X POST \
"http://localhost:4000/v1/events/logs?db=public&table=pipeline_logs&pipeline_name=greptime_identity" \
-H "Content-Type: application/json" \
-H "Authorization: Basic {{authentication}}" \
-d '[
{
"name": "Alice",
"age": 20,
"is_student": true,
"score": 90.5,
"object": { "a": 1, "b": 2 }
},
{
"age": 21,
"is_student": false,
"score": 85.5,
"company": "A",
"whatever": null
},
{
"name": "Charlie",
"age": 22,
"is_student": true,
"score": 95.5,
"array": [1, 2, 3]
}
]'
鉴权HTTP header。pipeline_name=greptime_identity指定了内置 pipeline。table=pipeline_logs指定了目标表。如果表不存在,将自动创建。greptime_identitypipeline 将自动为 JSON 日志中的每个字段创建列。成功执行命令将返回:
{"output":[{"affectedrows":3}],"execution_time_ms":9}
有关 greptime_identity pipeline 的更多详细信息,请参阅 写入日志 文档。
使用自定义 Pipeline 写入日志
自定义 pipeline 允许你解析结构的日志消息并将其转换为多列,并自动创建表。
创建 Pipeline
GreptimeDB 提供了一个专用的 HTTP 接口来创建 pipeline。方法如下:
首先,创建一个 pipeline 文件,例如 pipeline.yaml。
version: 2
processors:
- dissect:
fields:
- message
patterns:
- '%{ip_address} - - [%{timestamp}] "%{http_method} %{request_line}" %{status_code} %{response_size} "-" "%{user_agent}"'
ignore_missing: true
- date:
fields:
- timestamp
formats:
- "%d/%b/%Y:%H:%M:%S %z"
- select:
type: exclude
fields:
- message
transform:
- fields:
- ip_address
type: string
index: inverted
tag: true
- fields:
- status_code
type: int32
index: inverted
tag: true
- fields:
- request_line
- user_agent
type: string
index: fulltext
- fields:
- response_size
type: int32
- fields:
- timestamp
type: time
index: timestamp
该 pipeline 使用指定的模式拆分 message 字段以提取 ip_address、timestamp、http_method、request_line、status_code、response_size 和 user_agent。
然后,它使用格式 %d/%b/%Y:%H:%M:%S %z 解析 timestamp 字段,将其转换为数据库可以理解的正确时间戳格式。
最后,它将每个字段转换为适当的数据类型并相应地建立索引。
注意到在 pipeline 的最开始我们使用了版本 2 格式,详情请参考这个文档。
简而言之,在版本 2 下 pipeline 引擎会自动查找所有没有在 transform 模块中指定的字段,并使用默认的数据类型将他们持久化到数据库中。
你可以在后续章节中看到,虽然 http_method 没有在 transform 模块中被指定,但它依然被写入到了数据库中。
另外,select 处理器被用于过滤 原始的 message 字段。
需要注意的是,request_line 和 user_agent 字段被索引为 fulltext 以优化全文搜索查询,且表中必须有一个由 timestamp 指定的时间索引列。
执行以下命令上传配置文件:
curl -X "POST" \
"http://localhost:4000/v1/events/pipelines/nginx_pipeline" \
-H 'Authorization: Basic {{authentication}}' \
-F "file=@pipeline.yaml"
成功执行此命令后,将创建一个名为 nginx_pipeline 的 pipeline,返回的结果如下:
{"name":"nginx_pipeline","version":"2024-06-27 12:02:34.257312110Z"}.
你可以为同一 pipeline 名称创建多个版本。
所有 pipeline 都存储在 greptime_private.pipelines 表中。
请参阅查询 Pipelines以查看表中的 pipeline 数据。
写入日志
以下示例将日志写入 custom_pipeline_logs 表,并使用 nginx_pipeline pipeline 格式化和转换日志消息。
curl -X POST \
"http://localhost:4000/v1/events/logs?db=public&table=custom_pipeline_logs&pipeline_name=nginx_pipeline" \
-H "Content-Type: application/json" \
-H "Authorization: Basic {{authentication}}" \
-d '[
{
"message": "127.0.0.1 - - [25/May/2024:20:16:37 +0000] \"GET /index.html HTTP/1.1\" 200 612 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36\""
},
{
"message": "192.168.1.1 - - [25/May/2024:20:17:37 +0000] \"POST /api/login HTTP/1.1\" 200 1784 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36\""
},
{
"message": "10.0.0.1 - - [25/May/2024:20:18:37 +0000] \"GET /images/logo.png HTTP/1.1\" 304 0 \"-\" \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0\""
},
{
"message": "172.16.0.1 - - [25/May/2024:20:19:37 +0000] \"GET /contact HTTP/1.1\" 404 162 \"-\" \"Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1\""
}
]'
如果命令执行成功,你将看到以下输出:
{"output":[{"affectedrows":4}],"execution_time_ms":79}