【问题标题】:Elastic Search Bulk Import from JSON without ID弹性搜索从没有 ID 的 JSON 批量导入
【发布时间】:2016-10-30 15:42:14
【问题描述】:

有什么方法可以将 JSON 文件中的数据导入 elasticSearch,而无需为每个文档提供 ID?

我在 JSON 文件中有一些数据。它包含大约 1000 个文档,但没有为任何文档指定 ID。数据如下所示:


{"business_id": "aasd231as", "full_address": "202 McClure 15034", "hours":{}}
{"business_id": "123123444", "full_address": "1322 lure 34", "hours": {}}
{"business_id": "sd231as", "full_address": "2 McCl 5034", "hours": {}}

它在任何文档之前都没有{"index":{"_id":"5"}}。 现在我正在尝试使用以下命令将数据导入elasticsearch:

curl -XPOST localhost:9200/newindex/newtype/_bulk?pretty --data-binary @path/file.json

但它会引发以下错误:

"type" : "illegal_argument_exception",
"reason" : "Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]"

这是因为每个文档前面没有一行 ID。

有没有什么方法可以在每个文档前不提供{"index":{"_id":"5"}} 的情况下导入数据。 任何帮助将不胜感激!

【问题讨论】:

    标签: json elasticsearch import bulk


    【解决方案1】:

    另一种选择,也许更简单,因为您不过滤数据是使用 filebeat。最新的 filebeat-5.0.0-alpha3 有 JSON shipper。 Here 是一个示例

    【讨论】:

    • 我可以使用 filebeat 上传 2GB 的数据文件吗?
    【解决方案2】:

    如何使用非常适合此任务的 Logstash。只需使用以下配置文件即可完成:

    将以下配置保存在logstash.conf

    input {
      file {
       path => "/path/to/file.json"
       start_position => "beginning"
       sincedb_path => "/dev/null"
       codec => "json"
      }
    }
    filter {
     mutate {
      remove_field => [ "@version", "@timestamp", "path", "host" ]
     }
    }
    output {
     elasticsearch {
       hosts => ["localhost:9200"]
       index => "newindex"
       document_type => "newtype"
       workers => 1
     }
    }
    

    然后用

    启动logstash
    bin/logstash -f logstash.conf
    

    【讨论】:

    • 我尝试在logstash中使用配置,但它抛出了这个错误:不支持io/console; tty 不会被操作 信号 HUP 正在被 JVM 使用,在这个平台上不能正常工作
    • 错误:不支持 io/console; tty 不会被操作 信号 HUP 正在被 JVM 使用,在这个平台上不能正常工作
    • 你正在运行什么命令,你有什么版本的 Logstash?确保将上述配置保存在文件logstash.conf 中,然后运行bin/logstash -f logstash.conf
    • 错误已解决。现在它说,Pipeline Main Started。它是否加载了数据?我如何访问我的数据?
    • 没有错误是一个好兆头。如果你在curl -XGET localhost:9200/newindex/newtype/_search得到一些新数据,去检查你的 ES
    猜你喜欢
    • 2022-06-16
    • 2017-04-02
    • 2021-09-18
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    相关资源
    最近更新 更多