Logstash - 将嵌套 JSON 导入 Elasticsearch

Logstash - import nested JSON into Elasticsearch

我有大量 (~40000) 个嵌套 JSON 个对象,我想将其插入到 elasticsearch 索引中。

JSON 个对象的结构如下:

    {
    "customerid": "10932"
    "date": "16.08.2006",
    "bez": "xyz",
    "birthdate": "21.05.1990",
    "clientid": "2",
    "address": [
        {
            "addressid": "1",
            "tile": "Mr",
            "street": "main str",
            "valid_to": "21.05.1990",
            "valid_from": "21.05.1990",
        },
        {
            "addressid": "2",
            "title": "Mr",
            "street": "melrose place",
            "valid_to": "21.05.1990",
            "valid_from": "21.05.1990",
        }
      ]
    }

因此 JSON 字段(本例中的地址)可以包含 JSON 个对象的数组。

像这样将 JSON files/objects 导入 elasticsearch 的 logstash 配置会是什么样子?这个索引的 elasticsearch 映射应该看起来像 JSON 的结构。 elasticsearch 文档 id 应设置为 customerid.

input {
  stdin {
    id => "JSON_TEST"
  } 
}
filter {
    json{
        source => "customerid"
        ....
        ....    
    }

}
output {
       stdout{}
       elasticsearch {
          hosts => "https://localhost:9200/"
          index => "customers"           
          document_id => "%{customerid}"
       }                                               
}

如果您可以控制生成的内容,最简单的做法是将您的输入格式化为单行 json,然后使用 json_lines 编解码器。

只需将您的 stdin 更改为:

stdin { codec => "json_lines" }

然后它就会正常工作:

cat input_file.json | logstash -f json_input.conf

其中 input_file.json 有这样的行:

{"customerid":1,"nested": {"json":"here"}}
{"customerid":2,"nested": {"json":"there"}}

然后您将不需要 json 过滤器。