• Elasticsearch:Ingest pipeline 介绍


    Ingest pipeline 可让你在索引之前对数据执行常见转换。 例如,你可以使用 pipeline 删除字段、从文本中提取值并丰富你的数据。

    Pipeline 由一系列称为处理器(processors)的可配置任务组成。 每个处理器按顺序运行,对传入文档进行特定更改。 处理器运行后,Elasticsearch 会将转换后的文档添加到您的数据流或索引中。

    你可以使用 Kibana 的 Ingest Pipelines 功能或 ingest APIs 创建和管理摄取管道。 Elasticsearch 以集群状态存储管道。 

    前提条件

    在如下的展示中,我将使用 Elastic Stack 8.3.3 来进行展示,尽管不同的版本的界面可能稍有不同。

    创建及管理 pipeline

    在 Kibana 中,打开主菜单并单击 Stack Management > Ingest Pipelines。 从列表视图中,你可以:

    • 查看管道列表并深入了解详细信息
    • 编辑或克隆现有管道
    • 删除管道

     

    要创建管道,请单击上面的 Create pipeline > New pipeline。 有关示例教程,请参阅示例:ingest pipeline 使用示例 - 解析常用日志格式。 

    你还可以使用摄取 API 来创建和管理 pipeline。 以下创建 pipeline API 请求创建一个 pipeline,其中包含两个 set 处理器,后跟一个 lowercase 处理器。 处理器按指定的顺序依次运行。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "description": "My optional pipeline description",
    4. "processors": [
    5. {
    6. "set": {
    7. "description": "My optional processor description",
    8. "field": "my-long-field",
    9. "value": 10
    10. }
    11. },
    12. {
    13. "set": {
    14. "description": "Set 'my-boolean-field' to true",
    15. "field": "my-boolean-field",
    16. "value": true
    17. }
    18. },
    19. {
    20. "lowercase": {
    21. "field": "my-keyword-field"
    22. }
    23. }
    24. ]
    25. }

    在上面,我们定义了一个 pipeline。它在执行的时候是从上往下依次执行的。它为一个文档添加一个叫做 my-long-field 的字段,添加一个叫做 my-boolean-field 的字段,以及把 my-keyword-field 字段的字母都变为小写字母。我们可以使用如下的例子来进行检验:

    1. PUT my_index/_doc/1?pipeline=my-pipeline
    2. {
    3. "my-keyword-field": "Hi, this is Xiaoguo Liu"
    4. }

    在上面,我们创建一个叫做 my_index 的索引,并写入一个文件。我们可以使用如下的命令来检查写入的结果:

    GET my_index/_search?filter_path=hits.hits._source

    上面的命令返回的结果是:

    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_source": {
    6. "my-long-field": 10,
    7. "my-keyword-field": "hi, this is xiaoguo liu",
    8. "my-boolean-field": true
    9. }
    10. }
    11. ]
    12. }
    13. }

    显然,它和我们之前的所述的是一样的结果。我们可以在链接找到更多的 pipeline processors。我们甚至可以使用如下的 API 来获得所有的 pipeline processors:

    GET _nodes/ingest?filter_path=nodes.*.ingest.processors

    上面的命令显示为:

    1. {
    2. "nodes": {
    3. "EGibleagSe6UJMBgbuPbIA": {
    4. "ingest": {
    5. "processors": [
    6. {
    7. "type": "append"
    8. },
    9. {
    10. "type": "bytes"
    11. },
    12. {
    13. "type": "circle"
    14. },
    15. {
    16. "type": "community_id"
    17. },
    18. {
    19. "type": "convert"
    20. },
    21. {
    22. "type": "csv"
    23. },
    24. {
    25. "type": "date"
    26. },
    27. {
    28. "type": "date_index_name"
    29. },
    30. {
    31. "type": "dissect"
    32. },
    33. ...
    34. ]
    35. }
    36. }
    37. }
    38. }

    管理 pipeline 版本

    创建或更新 pipeline 时,可以指定可选的版本整数。 你可以将此版本号与 if_version 参数一起使用,以有条件地更新 pipeline。 当指定 if_version 参数时,成功的更新会增加 pipeline 的版本。

    1. PUT _ingest/pipeline/my-pipeline-id
    2. {
    3. "version": 1,
    4. "processors": [ ... ]
    5. }

    要使用 API 取消设置版本号,请在不指定版本参数的情况下替换或更新 pipeline。

    测试 pipeline

    在生产中使用 pipeline 之前,我们建议你i使用示例文档对其进行测试。 在 Kibana 中创建或编辑 pipeline 时,单击添加文档。 在 Documents 选项卡中,提供示例文档并单击 Run the pipeline。

    这个在我之前的文章 “Elasticsearch:ingest pipeline 使用示例 - 解析常用日志格式”  有详细的描述。

    你还可以使用模拟 pipeline API 测试 pipeline。 你可以在请求路径中指定配置的 pipeline。 例如,以下请求测试。

    1. POST _ingest/pipeline/_simulate
    2. {
    3. "pipeline": {
    4. "processors": [
    5. {
    6. "grok": {
    7. "description": "Extract fields from 'message'",
    8. "field": "message",
    9. "patterns": [
    10. """%{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \[%{HTTPDATE:@timestamp}\] "%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}"""
    11. ]
    12. }
    13. },
    14. {
    15. "date": {
    16. "description": "Format '@timestamp' as 'dd/MMM/yyyy:HH:mm:ss Z'",
    17. "field": "@timestamp",
    18. "formats": [
    19. "dd/MMM/yyyy:HH:mm:ss Z"
    20. ]
    21. }
    22. },
    23. {
    24. "geoip": {
    25. "description": "Add 'source.geo' GeoIP data for 'source.ip'",
    26. "field": "source.ip",
    27. "target_field": "source.geo"
    28. }
    29. },
    30. {
    31. "user_agent": {
    32. "description": "Extract fields from 'user_agent'",
    33. "field": "user_agent"
    34. }
    35. }
    36. ]
    37. },
    38. "docs": [
    39. {
    40. "_source": {
    41. "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
    42. }
    43. }
    44. ]
    45. }

    或者,你可以先创建一个 pipeline:

    1. PUT _ingest/pipeline/common_log_format
    2. {
    3. "description": "A pipeline to structure common logs ",
    4. "processors": [
    5. {
    6. "grok": {
    7. "field": "message",
    8. "patterns": [
    9. "%{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \\[%{HTTPDATE:@timestamp}\\] \"%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}\" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}"
    10. ]
    11. }
    12. },
    13. {
    14. "date": {
    15. "field": "@timestamp",
    16. "formats": [
    17. "dd/MMM/yyyy:HH:mm:ss Z"
    18. ],
    19. "output_format": "yyyy-MMM-dd'T'HH:mm:ss Z"
    20. }
    21. },
    22. {
    23. "geoip": {
    24. "field": "source.ip",
    25. "target_field": "source.geo"
    26. }
    27. },
    28. {
    29. "user_agent": {
    30. "field": "user_agent"
    31. }
    32. }
    33. ]
    34. }

    然后,我们再用如下的方法来进行测试:

    1. POST _ingest/pipeline/common_log_format/_simulate
    2. {
    3. "description": "A pipeline to structure common logs ",
    4. "processors": [
    5. {
    6. "grok": {
    7. "field": "message",
    8. "patterns": [
    9. """%{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \[%{HTTPDATE:@timestamp}\] "%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}"""
    10. ]
    11. }
    12. },
    13. {
    14. "date": {
    15. "field": "@timestamp",
    16. "formats": [
    17. "dd/MMM/yyyy:HH:mm:ss Z"
    18. ],
    19. "output_format": "yyyy-MMM-dd'T'HH:mm:ss Z"
    20. }
    21. },
    22. {
    23. "geoip": {
    24. "field": "source.ip",
    25. "target_field": "source.geo"
    26. }
    27. },
    28. {
    29. "user_agent": {
    30. "field": "user_agent"
    31. }
    32. }
    33. ],
    34. "docs": [
    35. {
    36. "_source": {
    37. "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
    38. }
    39. }
    40. ]
    41. }

    上面的命令将返回如下的结果:

    1. {
    2. "docs": [
    3. {
    4. "doc": {
    5. "_index": "_index",
    6. "_id": "_id",
    7. "_source": {
    8. "@timestamp": "2099-May-05T16:21:15 +0000",
    9. "http": {
    10. "request": {
    11. "method": "GET",
    12. "referrer": "\"-\""
    13. },
    14. "version": "1.1",
    15. "response": {
    16. "body": {
    17. "bytes": 3638
    18. },
    19. "status_code": 200
    20. }
    21. },
    22. "source": {
    23. "geo": {
    24. "continent_name": "Europe",
    25. "region_iso_code": "DE-BE",
    26. "city_name": "Berlin",
    27. "country_iso_code": "DE",
    28. "country_name": "Germany",
    29. "region_name": "Land Berlin",
    30. "location": {
    31. "lon": 13.3878,
    32. "lat": 52.5312
    33. }
    34. },
    35. "ip": "212.87.37.154"
    36. },
    37. "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"",
    38. "user": {
    39. "name": "-",
    40. "id": "-"
    41. },
    42. "url": {
    43. "original": "/favicon.ico"
    44. },
    45. "user_agent": {
    46. "name": "Chrome",
    47. "original": "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"",
    48. "os": {
    49. "name": "Mac OS X",
    50. "version": "10.11.6",
    51. "full": "Mac OS X 10.11.6"
    52. },
    53. "device": {
    54. "name": "Mac"
    55. },
    56. "version": "52.0.2743.116"
    57. }
    58. },
    59. "_ingest": {
    60. "timestamp": "2022-08-10T07:39:53.508450216Z"
    61. }
    62. }
    63. }
    64. ]
    65. }

    将 pipeline 添加到索引请求

    使用 pipeline 查询参数将管道应用于单个或批量索引请求中的文档。我们首先来创建一个 index template。这个 index template 含有 data stream

    1. PUT _index_template/my-data-stream-template
    2. {
    3. "index_patterns": [ "my-data-stream*" ],
    4. "data_stream": { },
    5. "priority": 500
    6. }

    我们使用如下的方法来把一些数据写入到一个叫做 my-data-stream 的 data stream 中去:

    1. POST my-data-stream/_doc?pipeline=my-pipeline
    2. {
    3. "@timestamp": "2099-03-07T11:04:05.000Z",
    4. "my-keyword-field": "foo"
    5. }
    6. PUT my-data-stream/_bulk?pipeline=my-pipeline
    7. { "create":{ } }
    8. { "@timestamp": "2099-03-07T11:04:06.000Z", "my-keyword-field": "foo" }
    9. { "create":{ } }
    10. { "@timestamp": "2099-03-07T11:04:07.000Z", "my-keyword-field": "bar" }

    我们可以通过如下的命令来查看写入的文档:

    GET my-data-stream/_search?filter_path=**.hits

    上面的命令返回的结果为:

    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_index": ".ds-my-data-stream-2022.08.10-000001",
    6. "_id": "Pai4hoIBT1laGVGsvcR4",
    7. "_score": 1,
    8. "_source": {
    9. "my-long-field": 10,
    10. "@timestamp": "2099-03-07T11:04:06.000Z",
    11. "my-keyword-field": "foo",
    12. "my-boolean-field": true
    13. }
    14. },
    15. {
    16. "_index": ".ds-my-data-stream-2022.08.10-000001",
    17. "_id": "Pqi4hoIBT1laGVGsvcR4",
    18. "_score": 1,
    19. "_source": {
    20. "my-long-field": 10,
    21. "@timestamp": "2099-03-07T11:04:07.000Z",
    22. "my-keyword-field": "bar",
    23. "my-boolean-field": true
    24. }
    25. },
    26. {
    27. "_index": ".ds-my-data-stream-2022.08.10-000001",
    28. "_id": "PKi4hoIBT1laGVGsssSh",
    29. "_score": 1,
    30. "_source": {
    31. "my-long-field": 10,
    32. "@timestamp": "2099-03-07T11:04:05.000Z",
    33. "my-keyword-field": "foo",
    34. "my-boolean-field": true
    35. }
    36. }
    37. ]
    38. }
    39. }

    你还可以将 pipeline 参数与通过 update_by_queryreindex  API 一起使用。

    1. POST my-data-stream/_update_by_query?pipeline=my-pipeline
    2. POST _reindex
    3. {
    4. "source": {
    5. "index": "my-data-stream"
    6. },
    7. "dest": {
    8. "index": "my-new-data-stream",
    9. "op_type": "create",
    10. "pipeline": "my-pipeline"
    11. }
    12. }

    设置默认 pipeline

    使用 index.default_pipeline 索引设置来设置默认 pipeline。 如果未指定 pipeline 参数,Elasticsearch 会将此 pipeline 应用于索引请求。比如,当我创建一个如下的索引:

    1. PUT my_index1
    2. {
    3. "settings": {
    4. "index.default_pipeline": "my-pipeline"
    5. }
    6. }
    7. PUT my_index1/_doc/1
    8. {
    9. "my-keyword-field": "FOO"
    10. }

    虽然在上面,在我们创建文档时,我们没有指定任何的 pipeline,但是它在默认的情况下,就自动将 pipeline 应用于该索引。我们使用如下的命令来查看:

    GET my_index1/_search?filter_path=**.hits
    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_index": "my_index1",
    6. "_id": "1",
    7. "_score": 1,
    8. "_source": {
    9. "my-long-field": 10,
    10. "my-keyword-field": "foo",
    11. "my-boolean-field": true
    12. }
    13. }
    14. ]
    15. }
    16. }

    设置最终 pipeline

    使用 index.final_pipeline 索引设置来设置最终 pipeline。 Elasticsearch 在请求或默认 pipeline 之后应用此 pipeline,即使两者都未指定。

    1. PUT _ingest/pipeline/my-final-pipeline
    2. {
    3. "description": "Increase my-long-field by 10",
    4. "processors": [
    5. {
    6. "script": {
    7. "source": """
    8. ctx['my-long-field'] += 10
    9. """
    10. }
    11. }
    12. ]
    13. }

    在上面,我们创建一个 my-final-pipeline 的 pipeline。它把字段 my-long-field 的值增加 10。能够完成这个操作的前提是 my-long-field 字段已经存在,否则我们的这个 pipeline 就好出问题。现在我们来创建一个叫做 my_index2 的索引:

    1. PUT my_index2
    2. {
    3. "settings": {
    4. "index.default_pipeline": "my-pipeline",
    5. "index.final_pipeline": "my-final-pipeline"
    6. }
    7. }

    在上面,我们定义了两个 pipeline:默认的及最终的。按照执行顺序来说,默认的先执行,然后才是 final。我们写入如下的文档:

    1. PUT my_index2/_doc/1
    2. {
    3. "my-keyword-field": "FOO"
    4. }

    我们通过如下的命令来进行查看:

    GET my_index2/_search?filter_path=**.hits

    上面的命令显示的结果为:

    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_index": "my_index2",
    6. "_id": "1",
    7. "_score": 1,
    8. "_source": {
    9. "my-long-field": 20,
    10. "my-keyword-field": "foo",
    11. "my-boolean-field": true
    12. }
    13. }
    14. ]
    15. }
    16. }

    显然,my-long-field 字段的值现在为 20,而不是之前的 10。

    在 Beats 中进行配置

    要将摄取 pipeline 添加到 Elastic Beats,请在 .yml 的 output.elasticsearch 下指定 pipeline 参数。 例如,对于 Filebeat,你可以在 filebeat.yml 中指定管道。

    1. output.elasticsearch:
    2. hosts: ["localhost:9200"]
    3. pipeline: my-pipeline

    用于 Fleet 和 Elastic Agent 的 pipeline

    Fleet 自动为其集成添加摄取 pipeline。 Fleet 使用包含为索引设置默认 pipeline 以及索引模板应用这些 pipeline。 Elasticsearch 根据 datastream 的命名方案将这些模板与你的 Fleet 数据流进行匹配。你可以阅读我之前的文章 “Observability:如何使用 Elastic Agents 把定制的日志摄入到 Elasticsearch 中”。

    警告:不要更改 Fleet 的摄取 pipeline 或使用自定义 pipeline 进行 Fleet 集成。 这样做可能会破坏你的 Fleet 数据流。

    Fleet 不为 Custom logs 集成提供摄取 pipeline。 你可以通过以下两种方式之一安全地为此集成指定 pipeline:index templatecustom configuraiton

    选项一:index template

    1)创建并测试你的摄取 pipeline。 将你的 pipeline 命名为 logs--default。 这使得跟踪集成 pipeline 变得更加容易。

    例如,以下请求为 my-app 数据集创建管道。 管道的名称是 logs-my_app-default。

    1. PUT _ingest/pipeline/logs-my_app-default
    2. {
    3. "description": "Pipeline for `my_app` dataset",
    4. "processors": [ ... ]
    5. }

    2)创建一个索引模板,在 index.default_pipeline 或 index.final_pipeline 索引设置中包含你的 pipeline。 确保模板已启用数据流。 模板的索引模式应该匹配 logs--*。

    你可以使用 Kibana 的索引管理功能或创建索引模板 API 创建此模板。

    例如,以下请求会创建一个匹配 logs-my_app-* 的模板。 该模板使用包含 index.default_pipeline 索引设置的组件模板。

    1. # Creates a component template for index settings
    2. PUT _component_template/logs-my_app-settings
    3. {
    4. "template": {
    5. "settings": {
    6. "index.default_pipeline": "logs-my_app-default",
    7. "index.lifecycle.name": "logs"
    8. }
    9. }
    10. }
    11. # Creates a mapping for index
    12. PUT _component_template/logs-my_app-mappings
    13. {
    14. "template": {
    15. "mappings": {
    16. "_source": {
    17. "enabled": false
    18. },
    19. "properties": {
    20. "host_name": {
    21. "type": "keyword"
    22. },
    23. "created_at": {
    24. "type": "date",
    25. "format": "EEE MMM dd HH:mm:ss Z yyyy"
    26. }
    27. }
    28. }
    29. }
    30. }
    31. # Creates an index template matching `logs-my_app-*`
    32. PUT _index_template/logs-my_app-template
    33. {
    34. "index_patterns": ["logs-my_app-*"],
    35. "data_stream": { },
    36. "priority": 500,
    37. "composed_of": ["logs-my_app-settings", "logs-my_app-mappings"]
    38. }

    3)在 Fleet 中添加或编辑 Custom logs 集成时,单击 Configure integration > Custom log file > Advanced options.。

    4)在数据集名称中,指定数据集的名称。 Fleet 会将用于集成的新数据添加到生成的 logs--default 数据流中。

    例如,如果你的数据集名称是 my_app,Fleet 会将新数据添加到 logs-my_app-default 数据流。

    5)使用 rollover API 翻转您的数据流。 这可确保 Elasticsearch 将索引模板及其 pipeline 设置应用于任何新数据以进行集成。

    选择二:custom configuration

    关于这个配置,我已经在我之前的文章  “Observability:如何使用 Elastic Agents 把定制的日志摄入到 Elasticsearch 中” 做了详尽的描述。这里就不再重复了。

    Elastic Agent standalone

    如果你独立运行 Elastic Agent,则可以使用包含 index.default_pipeline 或 index.final_pipeline 索引设置的索引模板应用管道。 或者,你可以在 elastic-agent.yml 配置中指定 pipeline 策略设置。 请参阅安装独立的 Elastic Agent

    访问处理器中的源字段

    处理器对传入文档的源字段具有读写访问权限。 要访问处理器中的字段键,请使用其字段名称。 以下 set 处理器访问 my-long-field。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "set": {
    6. "field": "my-long-field",
    7. "value": 10
    8. }
    9. }
    10. ]
    11. }

    你还可以添加 _source 前缀。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "set": {
    6. "field": "_source.my-long-field",
    7. "value": 10
    8. }
    9. }
    10. ]
    11. }

    使用点表示法访问对象字段。

    重要:如果你的文档包含展平对象,请先使用 dot_expander 处理器展开它们。 其他摄取处理器无法访问展平对象。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "dot_expander": {
    6. "description": "Expand 'my-object-field.my-property'",
    7. "field": "my-object-field.my-property"
    8. }
    9. },
    10. {
    11. "set": {
    12. "description": "Set 'my-object-field.my-property' to 10",
    13. "field": "my-object-field.my-property",
    14. "value": 10
    15. }
    16. }
    17. ]
    18. }

    比如,我们有如下的文档:

    1. PUT flattened_obj/_doc/1?pipeline=my-pipeline
    2. {
    3. "my-object-field.my-property": 100
    4. }

    最终写入的文档为:

    GET flattened_obj/_search?filter_path=**.hits
    1. {
    2. "hits": {
    3. "hits": [
    4. {
    5. "_index": "flattened_obj",
    6. "_id": "1",
    7. "_score": 1,
    8. "_source": {
    9. "my-object-field": {
    10. "my-property": 10
    11. }
    12. }
    13. }
    14. ]
    15. }
    16. }

    我们可以看出来 my-property 是 my-object-field 里的一个子字段。

    有几个处理器参数支持 Mustache 模板片段。 要访问模板片段中的字段值,请将字段名称括在三个大括号中:{{{field-name}}}。 你可以使用模板片段来动态设置字段名称。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "set": {
    6. "description": "Set dynamic '' field to 'code' value",
    7. "field": "{{{service}}}",
    8. "value": "{{{code}}}"
    9. }
    10. }
    11. ]
    12. }

    在处理器中访问摄取元数据

    摄取处理器可以使用 _ingest 键添加和访问摄取元数据。

    与源和元数据字段不同,Elasticsearch 默认不索引摄取元数据字段。 Elasticsearch 还允许以 _ingest 键开头的源字段。 如果你的数据包含此类源字段,请使用 _source._ingest 访问它们。

    默认情况下,pipelie 仅创建 _ingest.timestamp 摄取元数据字段。 该字段包含 Elasticsearch 收到文档索引请求的时间戳。 要索引 _ingest.timestamp 或其他摄取元数据字段,请使用 set 处理器。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "set": {
    6. "description": "Index the ingest timestamp as 'event.ingested'",
    7. "field": "event.ingested",
    8. "value": "{{{_ingest.timestamp}}}"
    9. }
    10. }
    11. ]
    12. }

    处理 pipeline 故障

    Pipeline 的处理器按顺序运行。 默认情况下,当这些处理器之一发生故障或遇到错误时,pipeline 处理将停止。

    要忽略处理器故障并运行管道的剩余处理器,请将 ignore_failure 设置为 true。详细阅读请参阅之前的文章 “Elasticsearch:如何处理 ingest pipeline 中的异常”。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "rename": {
    6. "description": "Rename 'provider' to 'cloud.provider'",
    7. "field": "provider",
    8. "target_field": "cloud.provider",
    9. "ignore_failure": true
    10. }
    11. }
    12. ]
    13. }

    使用 on_failure 参数指定在处理器发生故障后立即运行的处理器列表。 如果指定了 on_failure,即使 on_failure 配置为空,Elasticsearch 也会随后运行 pipeline 的剩余处理器。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "rename": {
    6. "description": "Rename 'provider' to 'cloud.provider'",
    7. "field": "provider",
    8. "target_field": "cloud.provider",
    9. "on_failure": [
    10. {
    11. "set": {
    12. "description": "Set 'error.message'",
    13. "field": "error.message",
    14. "value": "Field 'provider' does not exist. Cannot rename to 'cloud.provider'",
    15. "override": false
    16. }
    17. }
    18. ]
    19. }
    20. }
    21. ]
    22. }

    嵌套用于嵌套错误处理的 on_failure 处理器列表。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "rename": {
    6. "description": "Rename 'provider' to 'cloud.provider'",
    7. "field": "provider",
    8. "target_field": "cloud.provider",
    9. "on_failure": [
    10. {
    11. "set": {
    12. "description": "Set 'error.message'",
    13. "field": "error.message",
    14. "value": "Field 'provider' does not exist. Cannot rename to 'cloud.provider'",
    15. "override": false,
    16. "on_failure": [
    17. {
    18. "set": {
    19. "description": "Set 'error.message.multi'",
    20. "field": "error.message.multi",
    21. "value": "Document encountered multiple ingest errors",
    22. "override": true
    23. }
    24. }
    25. ]
    26. }
    27. }
    28. ]
    29. }
    30. }
    31. ]
    32. }

    你还可以为 pipeline 指定 on_failure。 如果没有 on_failure 值的处理器出现故障,Elasticsearch 会使用此 pipeline 级参数作为备用参数。 Elasticsearch 不会尝试运行 pipeline 的剩余处理器。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [ ... ],
    4. "on_failure": [
    5. {
    6. "set": {
    7. "description": "Index document to 'failed-'",
    8. "field": "_index",
    9. "value": "failed-{{{ _index }}}"
    10. }
    11. }
    12. ]
    13. }

    有关 pipeline 故障的其他信息可能在文档元数据字段 on_failure_message、on_failure_processor_type、on_failure_processor_tag 和 on_failure_pipeline 中可用。 这些字段只能从 on_failure 块中访问。

    以下示例使用元数据字段在文档中包含有关管道故障的信息。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [ ... ],
    4. "on_failure": [
    5. {
    6. "set": {
    7. "description": "Record error information",
    8. "field": "error_information",
    9. "value": "Processor {{ _ingest.on_failure_processor_type }} with tag {{ _ingest.on_failure_processor_tag }} in pipeline {{ _ingest.on_failure_pipeline }} failed with message {{ _ingest.on_failure_message }}"
    10. }
    11. }
    12. ]
    13. }

    有条件地运行处理器

    每个处理器都支持可选的 if 条件,编写为 Painless 脚本。 如果提供,则处理器仅在 if 条件为真时运行。

    重要:if 条件脚本在 Painless 的摄取处理器上下文中运行。 在 if 条件下,ctx 值是只读的。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "drop": {
    6. "description": "Drop documents with 'network.name' of 'Guest'",
    7. "if": "ctx?.network?.name == 'Guest'"
    8. }
    9. }
    10. ]
    11. }

    如果启用了 script.painless.regex.enabled 集群设置,你可以在 if 条件脚本中使用正则表达式。 有关支持的语法,请参阅 Painless 正则表达式

    提示:如果可能,请避免使用正则表达式。 昂贵的正则表达式会降低索引速度。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "set": {
    6. "description": "If 'url.scheme' is 'http', set 'url.insecure' to true",
    7. "if": "ctx.url?.scheme =~ /^http[^s]/",
    8. "field": "url.insecure",
    9. "value": true
    10. }
    11. }
    12. ]
    13. }

    你必须在一行中将 if 条件指定为有效 JSON。 但是,您可以使用 Kibana 控制台的三引号语法来编写和调试更大的脚本。

    提示:如果可能,请避免使用复杂或昂贵的 if 条件脚本。 昂贵的条件脚本会降低索引速度。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "drop": {
    6. "description": "Drop documents that don't contain 'prod' tag",
    7. "if": """
    8. Collection tags = ctx.tags;
    9. if(tags != null){
    10. for (String tag : tags) {
    11. if (tag.toLowerCase().contains('prod')) {
    12. return false;
    13. }
    14. }
    15. }
    16. return true;
    17. """
    18. }
    19. }
    20. ]
    21. }

    你还可以将存储的脚本指定为 if 条件。

    1. PUT _scripts/my-prod-tag-script
    2. {
    3. "script": {
    4. "lang": "painless",
    5. "source": """
    6. Collection tags = ctx.tags;
    7. if(tags != null){
    8. for (String tag : tags) {
    9. if (tag.toLowerCase().contains('prod')) {
    10. return false;
    11. }
    12. }
    13. }
    14. return true;
    15. """
    16. }
    17. }
    18. PUT _ingest/pipeline/my-pipeline
    19. {
    20. "processors": [
    21. {
    22. "drop": {
    23. "description": "Drop documents that don't contain 'prod' tag",
    24. "if": { "id": "my-prod-tag-script" }
    25. }
    26. }
    27. ]
    28. }

    传入文档通常包含对象字段。 如果处理器脚本尝试访问其父对象不存在的字段,Elasticsearch 将返回 NullPointerException。 要避免这些异常,请使用 null 安全运算符,例如 ?.,并将脚本编写为 null 安全的。

    例如, ctx.network?.name.equalsIgnoreCase('Guest') 不是 null 安全的。 ctx.network?.name 可以返回 null。 将脚本重写为 'Guest'.equalsIgnoreCase(ctx.network?.name),这是 null 安全的,因为 Guest 始终为非 null。

    如果你无法将脚本重写为空安全,请包含显式 null 检查。

    1. PUT _ingest/pipeline/my-pipeline
    2. {
    3. "processors": [
    4. {
    5. "drop": {
    6. "description": "Drop documents that contain 'network.name' of 'Guest'",
    7. "if": "ctx.network?.name != null && ctx.network.name.contains('Guest')"
    8. }
    9. }
    10. ]
    11. }

    有条件地应用管道

    将 if 条件与管道处理器相结合,以根据你的标准将其他管道应用于文档。 你可以将此管道用作用于配置多个数据流或索引的索引模板中的默认管道。

    1. PUT _ingest/pipeline/one-pipeline-to-rule-them-all
    2. {
    3. "processors": [
    4. {
    5. "pipeline": {
    6. "description": "If 'service.name' is 'apache_httpd', use 'httpd_pipeline'",
    7. "if": "ctx.service?.name == 'apache_httpd'",
    8. "name": "httpd_pipeline"
    9. }
    10. },
    11. {
    12. "pipeline": {
    13. "description": "If 'service.name' is 'syslog', use 'syslog_pipeline'",
    14. "if": "ctx.service?.name == 'syslog'",
    15. "name": "syslog_pipeline"
    16. }
    17. },
    18. {
    19. "fail": {
    20. "description": "If 'service.name' is not 'apache_httpd' or 'syslog', return a failure message",
    21. "if": "ctx.service?.name != 'apache_httpd' && ctx.service?.name != 'syslog'",
    22. "message": "This pipeline requires service.name to be either `syslog` or `apache_httpd`"
    23. }
    24. }
    25. ]
    26. }

    获取管道使用统计信息

    使用 node stats API 获取全局和每个 pipeline 的摄取统计信息。 使用这些统计信息来确定哪些 pipeline 运行最频繁或花费最多时间处理。

    GET _nodes/stats/ingest?filter_path=nodes.*.ingest
    1. {
    2. "nodes": {
    3. "EGibleagSe6UJMBgbuPbIA": {
    4. "ingest": {
    5. "total": {
    6. "count": 17,
    7. "time_in_millis": 7,
    8. "current": 0,
    9. "failed": 2
    10. },
    11. "pipelines": {
    12. "sample-pipeline": {
    13. "count": 0,
    14. "time_in_millis": 0,
    15. "current": 0,
    16. "failed": 0,
    17. "processors": []
    18. },
    19. "common_log_format": {
    20. "count": 2,
    21. "time_in_millis": 32,
    22. "current": 0,
    23. "failed": 0,
    24. "processors": [
    25. {
    26. "grok": {
    27. "type": "grok",
    28. "stats": {
    29. "count": 2,
    30. "time_in_millis": 6,
    31. "current": 0,
    32. "failed": 0
    33. }
    34. }
    35. },
    36. {
    37. "date": {
    38. "type": "date",
    39. "stats": {
    40. "count": 2,
    41. "time_in_millis": 3,
    42. "current": 0,
    43. "failed": 0
    44. }
    45. }
    46. },
    47. ...
  • 相关阅读:
    【C/C++】BMP格式32位转24位
    linux文件权限与目录配置
    【Mybatis源码】XMLConfigBuilder构建器 - 读取XML配置初始化Configuration对象
    尼莫地平纳米脂质体包载小干扰RNA(siRNA)|低分子肝素纳米脂质体包载信使RNA(mRNA)|齐岳生物
    家庭WIFI路由器、无线网卡购买指南
    搭环境太麻烦?试试一键跑个redis -- 环境准备
    uniapp h5文件流下载pdf文件
    maxwell源码编译安装部署
    Pr 时间重映射卡点
    Java Spring-AOP动态代理-JDK和CGlib实现
  • 原文地址:https://blog.csdn.net/UbuntuTouch/article/details/126242869