Link Search Menu Expand Document Documentation Menu

JSON 处理器

json 处理器将字符串值字段序列化为映射的映射,这对于各种数据处理和丰富任务非常有用。

以下是 json 处理器的语法:

{
  "processor": {
    "json": {
      "field": "<field_name>",
      "target_field": "<target_field_name>",
      "add_to_root": <boolean>
    }
  }
}

配置参数

下表列出了 json 处理器所需和可选的参数。

参数 必需/可选 描述
字段 必需 包含要反序列化的 JSON 格式字符串的字段名称。
目标字段 可选 存储反序列化 JSON 数据的字段名称。如果未提供,数据将存储在 field 字段中。如果 target_field 存在,其现有值将被新的 JSON 数据覆盖。
add_to_root 可选 一个布尔标志,用于确定反序列化的 JSON 数据应添加到文档的根 (true) 还是存储在 target_field (false) 中。如果 add_to_roottrue,则 target-field 无效。默认值为 false
description 可选 处理器的目的或配置说明。
if 可选 指定条件性执行处理器。
ignore_failure 可选 指定忽略处理器失败。请参阅处理管道失败
on_failure 可选 指定一个处理器列表,以便在处理器执行失败时运行。这些处理器按指定的顺序执行。
tag 可选 处理器的标识符标签。有助于调试以区分相同类型的处理器。

使用处理器

按照以下步骤在管道中使用处理器。

步骤 1:创建管道

以下查询创建了一个名为 my-json-pipeline 的管道,该管道使用 json 处理器处理 JSON 数据并用额外信息丰富文档

PUT _ingest/pipeline/my-json-pipeline
{
  "description": "Example pipeline using the JsonProcessor",
  "processors": [
    {
      "json": {
        "field": "raw_data",
        "target_field": "parsed_data"
        "on_failure": [
          {
            "set": {
              "field": "error_message",
              "value": "Failed to parse JSON data"
            }
          },
          {
            "fail": {
              "message": "Failed to process JSON data"
            }
          }
        ]
      }
    },
    {
      "set": {
        "field": "processed_timestamp",
        "value": ""
      }
    }
  ]
}

步骤 2(可选):测试管道

建议在摄取文档之前测试您的管道。

要测试管道,请运行以下查询

POST _ingest/pipeline/my-json-pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "raw_data": "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
      }
    },
    {
      "_source": {
        "raw_data": "{\"name\":\"Jane\",\"age\":25,\"city\":\"Los Angeles\"}"
      }
    }
  ]
}

响应

以下示例响应确认管道按预期工作

{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "processed_timestamp": "2024-05-30T15:24:48.064472090Z",
          "raw_data": """{"name":"John","age":30,"city":"New York"}""",
          "parsed_data": {
            "name": "John",
            "city": "New York",
            "age": 30
          }
        },
        "_ingest": {
          "timestamp": "2024-05-30T15:24:48.06447209Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "processed_timestamp": "2024-05-30T15:24:48.064543006Z",
          "raw_data": """{"name":"Jane","age":25,"city":"Los Angeles"}""",
          "parsed_data": {
            "name": "Jane",
            "city": "Los Angeles",
            "age": 25
          }
        },
        "_ingest": {
          "timestamp": "2024-05-30T15:24:48.064543006Z"
        }
      }
    }
  ]
}

步骤 3:摄取文档

以下查询将文档摄入到名为 my-index 的索引中

POST my-index/_doc?pipeline=my-json-pipeline
{
  "raw_data": "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
}

响应

响应确认包含来自 raw_data 字段的 JSON 数据的文档已成功索引

{
  "_index": "my-index",
  "_id": "mo8yyo8BwFahnwl9WpxG",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 2
}

步骤 4(可选):检索文档

要检索文档,请运行以下查询

GET my-index/_doc/1

剩余 350 字符

有问题?

想贡献?