簡體   English   中英

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 文件/對象導入 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過濾器了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM