【问题标题】:Importing JSON in to Elasticsearch 5.1 using CURL使用 CURL 将 JSON 导入 Elasticsearch 5.1
【发布时间】:2023-03-14 05:58:01
【问题描述】:

我正在尝试将大型 JSON 文档导入 Elasticsearch 5.1。一小部分数据如下所示:

[
    {
      "id": 1,
      "region": "ca-central-1",
      "eventName": "CreateRole",
      "eventTime": "2016-02-04T03:41:19.000Z",
      "userName": "email@group.com"
    },
    {
      "id": 2,
      "region": "ca-central-1",
      "eventName": "AddRoleToInstanceProfile",
      "eventTime": "2016-02-04T03:41:19.000Z",
      "userName": "email@group.com"
    },
    {
      "id": 3,
      "region": "ca-central-1",
      "eventName": "CreateInstanceProfile",
      "eventTime": "2016-02-04T03:41:19.000Z",
      "userName": "email@group.com"
    },
    {
      "id": 4,
      "region": "ca-central-1",
      "eventName": "AttachGroupPolicy",
      "eventTime": "2016-02-04T01:42:36.000Z",
      "userName": "email@group.com"
    },
    {
      "id": 5,
      "region": "ca-central-1",
      "eventName": "AttachGroupPolicy",
      "eventTime": "2016-02-04T01:39:20.000Z",
      "userName": "email@group.com"
    }
]

如果可能的话,我想在不对源数据进行任何更改的情况下导入数据,因此我认为排除了 _bulk 命令,因为我需要为每个条目添加额外的详细信息。

我尝试了几种不同的方法,但都没有运气。我是否在浪费时间尝试按原样导入此文档?

我试过了:

curl -XPOST 'demo.ap-southeast-2.es.amazonaws.com/rea/test' --data-binary @Records.json

但是失败并出现错误:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}},"status":400}

谢谢!

【问题讨论】:

    标签: json curl elasticsearch


    【解决方案1】:

    如果您不想修改文件,批量 api 将不起作用。

    你可以看看jq。 它是一个命令行 json 解析器。它可能会帮助您生成运行批量 api 所需的文档。

    cat Records.json | 
    jq -c '
    .[] |
    { index: { _index: "index_name", _type: "type_name" } },
    . '
    

    您可以尝试这样的事情并将其传递给批量 api。希望这会有所帮助。

    您也可以尝试进行类似这样的 curl 调用。

    cat Records.json | 
    jq -
    .[] |
    { index: { _index: "index_name", _type: "type_name" } },
    . ' | curl -XPOST demo.ap-southeast-2.es.amazonaws.com/_bulk --data-binary @-
    

    没有尝试过第二部分,但应该可以。

    【讨论】:

    • 感谢您的回复。我会尝试这些选项并报告!
    【解决方案2】:

    您可能想查看stream2es - 这是一个用于将文档发送到 ElasticSearch 的有用实用程序。我认为它可能会做你需要做的事情。

    一旦你安装了它,你应该可以像这样使用它:

    cat Records.json | ./stream2es stdin --target 'http://demo.ap-southeast-2.es.amazonaws.com/rea/test'
    

    【讨论】:

    • 感谢您的回复。我也会试试stream2es!
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 2016-03-06
    相关资源
    最近更新 更多