logstash-将嵌套的JSON导入Elasticsearch



我有大量(〜40000(的嵌套json对象,我想将其插入Elasticsearch index。

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对象的数组。

LogStash配置将如何将JSON文件/对象导入Elasticsearch?此索引的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过滤器。

最新更新