整体思路与架构 在 Elasticsearch 中,数据清洗与预处理的最佳位置是写入前的 Ingest Pipeline:在 摄取节点(Ingest Node) 上定义由多个 处理器(Processor) 组成的处理链,对文档进行清洗、转换、富化后再写入索引。这样可统一处理逻辑、降低应用复杂度,并便于观测与调试。典型数据流为:数据源 → Beats/Logstash → Ingest Pipeline → 索引;也可直接在索引或批量请求中指定管道执行。
常用处理器与能力
端到端实战示例
目标:将原始日志行解析为结构化字段,统一时间、标准化大小写、丰富地理信息,并做容错与去重准备
步骤
示例
# 1) 创建 enrich policy(示例)
PUT _enrich/policy/ip_geo_policy
{
"match": {
"indices": ["geoip-data-*"],
"match_field": "ip",
"enrich_fields": ["city_name", "country_name", "location"]
}
}
# 2) 创建 ingest pipeline
PUT _ingest/pipeline/weblog_pipeline
{
"description": "Weblog clean, parse, normalize and enrich",
"processors": [
{ "trim": { "field": "message" } },
{
"grok": {
"field": "message",
"patterns": [
"%{IP:client_ip} %{WORD:method} %{URIPATHPARAM:request} HTTP/%{NUMBER:http_version} %{NUMBER:status:integer} %{NUMBER:bytes:long} %{HTTPDATE:timestamp}"
],
"ignore_failure": true
}
},
{
"date": {
"field": "timestamp",
"formats": ["dd/MMM/yyyy:HH:mm:ss Z"],
"target_field": "@timestamp"
}
},
{ "lowercase": { "field": "method" } },
{ "remove": { "field": "message", "ignore_missing": true } },
{
"geoip": {
"field": "client_ip",
"target_field": "geo",
"ignore_missing": true
}
},
{
"enrich": {
"policy_name": "ip_geo_policy",
"field": "client_ip",
"target_field": "geo_enriched",
"ignore_missing": true
}
},
{
"fingerprint": {
"fields": ["client_ip","request","@timestamp"],
"target_field": "doc_fingerprint"
}
},
{
"on_failure": [
{
"set": {
"field": "ingest_error",
"value": "{{ _ingest.on_failure_message }}"
}
}
]
}
]
}
# 3) 模拟管道
POST _ingest/pipeline/weblog_pipeline/_simulate
{
"docs": [
{
"_source": {
"message": " 203.0.113.10 GET /search?q=elastic HTTP/1.1 200 1024 10/Oct/2024:13:55:36 +0000 "
}
}
]
}
# 4) 写入时指定管道
PUT logs-web-000001/_doc/1?pipeline=weblog_pipeline
{
"message": "203.0.113.10 GET /search?q=elastic HTTP/1.1 200 1024 10/Oct/2024:13:55:36 +0000"
}
# 5) 批量导入(NDJSON,指定管道)
POST _bulk?pipeline=weblog_pipeline
{ "index": { "_index": "logs-web-000001" } }
{ "message": "198.51.100.23 POST /login HTTP/1.1 302 512 11/Nov/2024:08:20:11 +0000" }
上述示例覆盖了 Grok 解析、Date 标准化、GeoIP 富化、Fingerprint 去重准备、on_failure 容错 等常见清洗与预处理需求。
导入与调试建议
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。