在今天的文章中,我们将讲述如何运用Elasticsearch的 ingest 节点来对数据进行结构化并对数据进行处理。
数据集
在我们的实际数据采集中,数据可能来自不同的来源,并且以不同的形式展展现:
这些数据可以是一种很结构化的数据被摄,比如数据库中的数据, 或者就是一直最原始的非结构化的数据,比如日志。对于一些非结构化的数据,我们该如何把它们结构化,并使用Elasticsearch进行分析呢?
结构化数据
就如上面的数据展示的那样。在很多的情况下,数据在摄入的时候是一种非结构化的形式来呈现的。这个数据通常有一个叫做message的字段。为了能达到结构化的目的,我们们需要parse及transform这个message字段,并把这个message变为我们所需要的字段,从而达到结构化的母的。让我们看一个例子。假如我们有如下的信息:
{
"message": "2019-09-29T00:39:02.9122 [Debug] MyApp stopped"
}
显然上面的信息是一个非结构化的信息。它含有唯一的一个字段message。我们希望通过一些方法把它变成为:
{
"@timestamp": "2019-09-29T00:39:02.9122",
"loglevel": "Debug",
"status": "MyApp stopped"
}
显然上面的数据是一个结构化的文档。它更便于我们对数据进行分析。比如我们对数据进行聚合或在Kibana中进行展示。
我们接下来看一下一个典型的Elastic Stack的架构图:
在上面,我们可以看到有两个地方我们可以对数据进行处理:
我们可以使用Logstash和Ingest node来对我们的数据进行处理。如果大家还对使用Logstash或者是Ingest Node没法做选择的话,请参阅我之前的文章“我应该使用Logstash或是Elasticsearch ingest 节点?”。
如果你的日志数据不是一个已有的格式,比如apache, nginx,那么你需要建立自己的pipeline来对这些日志进行处理。在今天的文章里,我们将介绍如何使用Elasticsearch的ingest processors来对我们的非结构化数据进行处理,从而把它们变为结构化的数据:
- split
- dissect
- kv
- grok
- ...
Ingest pipelines
一个Elasticsearch pipeline是一组processors:
- 让我们在数据建立索引之前做预处理
- 每一个processor可以修改经过它的文档
- processor的处理是在Elasticsearch新的ingest node里进行的
定义一个Elasticsearch的ingest pipeline
我们可以使用Ingest API来定义pipelines:
我们可以使用_simulate重点来进行测试:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"split": {
"field": "message",
"separator": " "
}
}
]
},
"docs": [
{
"_source": {
"message": "2019-09-29T00:39:02.912Z AppServer1 STATUS_OK"
}
}
]
}
上面的运行的结果是:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"message" : [
"2019-09-29T00:39:02.912Z",
"AppServer1",
"STATUS_OK"
]
},
"_ingest" : {
"timestamp" : "2020-04-27T08:40:43.059569Z"
}
}
}
]
}
如何使用Pipeline
一旦你定义好一个pipeline,如果你是使用Filebeat接入到Elasticsearch导入数据,那么你可以在filebeat的配置文件中这样使用这个pipeline:
output.elasticsearch:
hosts: ["http://localhost:9200"]
pipeline: my_pipeline
你也可以直接为你的Elasticsearch index定义一个默认的pipeline:
PUT my_index
{
"settings": {
"default_pipeline": "my_pipeline"
}
}
这样当我们的数据导入到my_index里去的时候,my_pipeline将会被自动调用。
例子
Dissect
我们下面来看一个更为复杂一点的例子。你需要同时使用split及kv processor来结构化这个消息:
正如我们上面显示的那样,我们想提取上面用红色标识的部分,但是我们并不需要信息中中括号【 及 】。我可以使用dissect processor:
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "Example using dissect processor",
"processors": [
{
"dissect": {
"field": "message",
"pattern": "%{@timestamp} [%{loglevel}] %{status}"
}
}
]
},
"docs": [
{
"_source": {
"message": "2019-09-29T00:39:02.912Z [Debug] MyApp stopped"
}
}
]
}
上面显示的结果是:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"@timestamp" : "2019-09-29T00:39:02.912Z",
"loglevel" : "Debug",
"message" : "2019-09-29T00:39:02.912Z [Debug] MyApp stopped",
"status" : "MyApp stopped"
},
"_ingest" : {
"timestamp" : "2020-04-27T09:10:33.720665Z"
}
}
}
]
}
我们接下来显示一个key-value对的信息:
{
"message": "2019009-29T00:39:02.912Z host=AppServer status=STATUS_OK"
}
我们同样可以使用dissect processor来处理:
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "Example using dissect processor key-value",
"processors": [
{
"dissect": {
"field": "message",
"pattern": "%{@timestamp} %{*field1}=%{&field1} %{*field2}=%{&field2}"
}
}
]
},
"docs": [
{
"_source": {
"message": "2019009-29T00:39:02.912Z host=AppServer status=STATUS_OK"
}
}
]
}
在上面,*及&是参考键修饰符,它们用来改变dissect的行为。上面的结果显示:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"@timestamp" : "2019009-29T00:39:02.912Z",
"host" : "AppServer",
"message" : "2019009-29T00:39:02.912Z host=AppServer status=STATUS_OK",
"status" : "STATUS_OK"
},
"_ingest" : {
"timestamp" : "2020-04-27T14:04:38.639127Z"
}
}
}
]
}
Script processor
尽管现有的很多的processor都能给我们带来很大的方便,但是在实际的应用中,有很多的能够并不在我们的Logstash或Elasticsearch预设的功能之列。一种办法就是写自己的插件,但是这可能是一件巨大的任务。我们可以写一个脚本来完成这个工作。通常这个是由Elasticsearch的Painless脚本来完成的。如果你想了解更多的Painless的知识,你可以在“Elastic:菜鸟上手指南”找到几篇这个语言的介绍文章。
有两种方法可以允许Painless script:inline或者stored。
Inline scripts
在下面的例子中它展示的是一个inline的脚本,用来更新一个叫做new_field的字段:
PUT /_ingest/pipeline/my_script_pipeline
{
"processors": [
{
"script": {
"source": "ctx['new_field'] = params.value",
"params": {
"value": "Hello world"
}
}
}
]
}
在上面,我们使用params来把参数传入。这样做的好处是source的代码一直是没有变化的,这样它只会被编译一次。如果source的代码随着调用的不同而改变,那么它将会被每次编译从而造成浪费。
Stored scripts
Scripts也可以保存于Cluster的状态中,并且在以后引用script的ID来调用:
PUT _scripts/my_script
{
"script": {
"lang": "painless",
"source": "ctx['new_field'] = params.value"
}
}
PUT /_ingest/pipeline/my_script_pipeline
{
"processors": [
{
"script": {
"id": "my_script",
"params": {
"value": "Hello world!"
}
}
}
]
}
上面的两个命令将实现和之前一样的功能。当我们在ingest node使用场景的时候,我们访问文档的字段时,使用cxt['new_field']。我们也可以访问它的元字段,比如cxt['_id'] = ctx['my_field']。
我们先来做几个练习:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"lang": "painless",
"source": "ctx['new_value'] = ctx['current_value'] + 1"
}
}
]
},
"docs": [
{
"_source": {
"current_value": 2
}
}
]
}
上面的脚本运行时会生产一个新的叫做new_value的字段,并且它的值将会是由curent_value字段的值加上1。运行上面的结果是:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"new_value" : 3,
"current_value" : 2
},
"_ingest" : {
"timestamp" : "2020-04-27T14:49:35.775395Z"
}
}
}
]
}
我们接下来一个例子就是来创建一个stored script:
PUT _scripts/my_script
{
"script": {
"lang": "painless",
"source": "ctx['new_value'] = ctx['current_value'] + params.value"
}
}
PUT /_ingest/pipeline/my_script_pipeline
{
"processors": [
{
"script": {
"id": "my_script",
"params": {
"value": 1
}
}
}
]
}
上面的这个语句和之前的那个实现的是同一个功能。我们先执行上面的两个命令。为了能测试上面的pipeline是否工作,我们尝试创建两个文档:
POST test_docs/_doc
{
"current_value": 34
}
POST test_docs/_doc
{
"current_value": 80
}
然后,我们运行如下的命令:
POST test_docs/_update_by_query?pipeline=my_script_pipeline
{
"query": {
"range": {
"current_value": {
"gt": 30
}
}
}
}
在上面,我们通过使用_update_by_query结合pipepline一起来更新我们的文档。我们只针对 current_value大于30的文档才起作用。运行完后:
{
"took" : 25,
"timed_out" : false,
"total" : 2,
"updated" : 2,
"deleted" : 0,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
它显示已经更新两个文档了。我们使用如下的语句来进行查看:
GET test_docs/_search
显示的结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_docs",
"_type" : "_doc",
"_id" : "EIEnvHEBQHMgxFmxZyBq",
"_score" : 1.0,
"_source" : {
"new_value" : 35,
"current_value" : 34
}
},
{
"_index" : "test_docs",
"_type" : "_doc",
"_id" : "D4EnvHEBQHMgxFmxXyBp",
"_score" : 1.0,
"_source" : {
"new_value" : 81,
"current_value" : 80
}
}
]
}
}
从上面我们可以看出来new_value字段的值是current_value字段的值加上1。
我们再接着看如下的例子:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"split": {
"field": "message",
"separator": " ",
"target_field": "split_message"
}
},
{
"set": {
"field": "environment",
"value": "prod"
}
},
{
"set": {
"field": "@timestamp",
"value": "{{split_message.0}}"
}
}
]
},
"docs": [
{
"_source": {
"message": "2019-09-29T00:39:02.912Z AppServer1 STATUS_OK"
}
}
]
}
在上面第一个split processor,我们把message按照" "来进行拆分,并同时把结果赋予给字段split_message。它其实是一个数组。接着我们通过set processor添加一个叫做environment的字段,并赋予值prod。再接着我们把split_message数组里的第一个值拿出来赋予给@timestamp字段。这是一个添加的字段。运行的结果如下:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"environment" : "prod",
"@timestamp" : "2019-09-29T00:39:02.912Z",
"message" : "2019-09-29T00:39:02.912Z AppServer1 STATUS_OK",
"split_message" : [
"2019-09-29T00:39:02.912Z",
"AppServer1",
"STATUS_OK"
]
},
"_ingest" : {
"timestamp" : "2020-04-27T15:35:00.922037Z"
}
}
}
]
}
Grok processor
Grok processor提供了一种正则匹配的方式让我们把pattern和message进行匹配,从而提前出message里的结构化数据:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"grok": {
"field": "message",
"patterns": [
"%{TIMESTAMP_ISO8601:@timestamp} %{IP:client} \\[%{WORD:status}\\] %{NUMBER:duration}"
]
}
}
]
},
"docs": [
{
"_source": {
"message": "2019-09-29T00:39:02.912Z 55.3.241.1 [OK] 0.043"
}
}
]
}
上面的返回结果是:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"duration" : "0.043",
"@timestamp" : "2019-09-29T00:39:02.912Z",
"client" : "55.3.241.1",
"message" : "2019-09-29T00:39:02.912Z 55.3.241.1 [OK] 0.043",
"status" : "OK"
},
"_ingest" : {
"timestamp" : "2020-04-28T00:16:52.155688Z"
}
}
}
]
}
Grok processro也对多行的事件也可以处理的很好。比如:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"grok": {
"field": "text",
"patterns": ["%{GREEDYMULTILINE:allMyData}"],
"pattern_definitions": {
"GREEDYMULTILINE": "(.|\n)*"
}
}
}
]
},
"docs": [
{
"_source": {
"text": "This is a text \n secondline"
}
}
]
}
上面运行的结果是:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"text" : """This is a text
secondline""",
"allMyData" : """This is a text
secondline"""
},
"_ingest" : {
"timestamp" : "2020-04-28T00:31:38.913929Z"
}
}
}
]
}
在上面我们可以看到allMydata把多行的数据都提前到同一个字段。在上面如果我们只用其中的一种 pattern_definitions,比如 .*:
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"grok": {
"field": "text",
"patterns": ["%{GREEDYMULTILINE:allMyData}"],
"pattern_definitions": {
"GREEDYMULTILINE": ".*"
}
}
}
]
},
"docs": [
{
"_source": {
"text": "This is a text \n secondline"
}
}
]
}
那么我们可以看到:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"text" : """This is a text
secondline""",
"allMyData" : "This is a text "
},
"_ingest" : {
"timestamp" : "2020-04-28T00:35:59.67759Z"
}
}
}
]
}
也就是它只提前了第一行。
Date processor
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"date": {
"field": "date",
"formats": [
"MM/dd/yyyy HH:mm",
"dd-MM-yyyy HH:mm:ssz"
]
}
}
]
},
"docs": [
{
"_source": {
"date": "03/25/2019 03:39"
}
},
{
"_source": {
"date": "25-03-2019 03:39:00+01:00"
}
}
]
}
在上面我们定义了两种时间的格式,如果其中的一个有匹配,那么时间将会被正确地解析,同时被自动赋予给@timestamp字段。这个和Logstash的date processor是一样的。上面运行的结果是:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"date" : "03/25/2019 03:39",
"@timestamp" : "2019-03-25T03:39:00.000Z"
},
"_ingest" : {
"timestamp" : "2020-04-28T00:24:24.802381Z"
}
}
},
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"date" : "25-03-2019 03:39:00+01:00",
"@timestamp" : "2019-03-25T02:39:00.000Z"
},
"_ingest" : {
"timestamp" : "2020-04-28T00:24:24.802396Z"
}
}
}
]
}