如何将大 JSON 文件导入到带有 ELK 堆栈的 Docker-swarm 集群?

How to import a big JSON-file to a Docker-swarm cluster with ELK stack?

基本上我想将 JSON-data 导入 (Logstash->ElasticSearch->) Kibana,但我是全新的并且坚持使用不同的方法,我不完全理解并得到错误或无输出。

我得到的是一个文件 test.json,其中包含以下格式的维基百科数据:

{
"results": [
    {
        "curr": "Ohio_\"Heartbeat_Bill\"",
        "n": 43,
        "prev": "other-external",
        "type": "external"
    },
    {
        "curr": "Ohio_\"Heartbeat_Bill\"",
        "n": 1569,
        "prev": "other-search",
        "type": "external"
    },
    {
        "curr": "Ohio_\"Heartbeat_Bill\"",
        "n": 11,
        "prev": "other-internal",
        "type": "external"
    },
...

等等。该文件有 1.3Mb 大,因为我删除了一些最大的示例。

我尝试了 curl 命令:

cat test.json | jq -c '.[] | {"index": {}}, .' | curl -XPOST localhost:9200/_bulk --data-binary @-

curl -s -XPOST localhost:9200/_bulk --data-binary @test.json

write "{ "index" : { } }" at the beginning of the document

我也试过:

curl -XPUT http://localhost:9200/wiki -d '
{
  "mappings" : {
    "_default_" : {
      "properties" : {
        "curr" : {"type": "string"},
        "n" : {"type": "integer"},
        "prev" : {"type": "string"},
        "type" : {"type": "string"}
      }
    }
  }
}
';

但我总是得到这个错误:

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

或者当我使用:

curl localhost:9200/wiki -H "Content-type:application/json" -X POST -d @test.json

我得到:

{"error":"Incorrect HTTP method for uri [/wiki] and method [POST], allowed: [GET, HEAD, DELETE, PUT]","status":405}

当我将 "wiki" 替换为“_bulk”时,就像所有示例似乎都有的共同点一样,我得到:

{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication token for REST request [/_bulk]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication token for REST request [/_bulk]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401

据我所知,我还复制粘贴并调整了 Kibana-Logstash-Pipeline 中的 conf 文件,如下所示:

input 
{
    file 
    {
        codec => multiline
        {
            pattern=> '^\{'
            negate=> true
            what=> previous
        }
        path => ["/home/user/docker-elastic/examples/pretty.json"]
        start_position => "beginning"
        sincedb_path => "/dev/null"
        exclude => "*.gz"
    }
}

filter 
{
    mutate
    {
        replace => [ "message", "%{message}}" ]
        gsub => [ 'message','\n','']
    }
    if [message] =~ /^{.*}$/ 
    {
        json { source => message }
    }
}

output
{ 
  elasticsearch {
        protocol => "http"
        codec => json
        host => "localhost"
        index => "wiki_json"
        embedded => true
    }

    stdout { codec => rubydebug }
}

但是当我点击 "create and deploy" 时没有任何反应。

所以我尝试了一些示例,但就像我说的那样 - 我并不完全理解它们,因此无法将我的数据传输到 Kibana。我写过 Logstash 和 ElasticSearch,因为我也很想用它们来传递数据。

有人可以向我解释一下,我如何才能直接传递这些数据,而无需手动更改文件?许多答案说数据不能在我的结构中传递,但必须是 "one line, one input"-only。但是我无法手动更改包含近 40000 个数据的整个文件,我不想为它编写 python-script..

也许有工具之类的?或者也许我太笨了,无法理解语法并且做错了什么?

感谢任何帮助! 先感谢您!

就像@Ian Kemp 在评论部分回答的那样,问题是我使用了 POST 而不是 PUT。之后我得到一个错误说认证失败,所以我用谷歌搜索并得到了最终答案:

curl elastic:changeme@localhost:9200/wiki -H "Content-type: application/json" -X PUT -d @test.json

与文件中的索引行。 这是我最终将数据放入 Elasticsearch 的结构:) 非常感谢 Ian Kemp!