【问题标题】:Add a header line before each json document在每个 json 文档之前添加一个标题行
【发布时间】:2016-11-14 19:31:44
【问题描述】:

我有一个包含 1000 个 json 对象的 json 文件。 有没有办法在每个 json 文档之前添加标题行?有没有最简单的方法?

示例:我有 1000 个这样的对象

{"id":58,"first_name":"Louis","last_name":"Jordan","email":"ljordan1l@nature.com","gender":"Male","Latitude":"-15.93444","Longitude":"-50.14028"}

我想为每个 json 对象添加如下索引头,以便我可以在 Elasticsearch Bulk api 中使用

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "unique_id" } }
{"id":58,"first_name":"Louis","last_name":"Jordan","email":"ljordan1l@nature.com","gender":"Male","Latitude":"-15.93444","Longitude":"-50.14028"}

【问题讨论】:

  • 你想指定自己的ID,还是让ES自动为你生成?
  • 无论是 ID,自动生成还是我的特定。

标签: json elasticsearch


【解决方案1】:

如果您愿意利用 Logstash,则无需修改文件,只需逐行读取并使用利用 Bulk API 的 elasticsearch 输出将其流式传输到 ES。

将以下 Logstash 配置存储在名为 es.conf 的文件中(确保文件 path 和 ES hosts 与您的设置匹配):

input {
  file {
    path => "/path/to/your/json"
    sincedb_path => "/dev/null"
    start_position => "beginning"
    codec => "json"
  }
}
filter {
  mutate {
    remove_fields => ["@version", "@timestamp"]
  }
}
output {
  elasticsearch {
    hosts => "localhost:9200"
    index => "test"
    document_type => "type1"
    document_id => "%{id}"
  }
}

然后,您需要install logstash 并且您将能够运行以下命令以将您的 JSON 文件加载到您的 ES 服务器:

bin/logstash -f es.conf

【讨论】:

  • 我怎样才能运行这个 sn-p ?
【解决方案2】:

我找到了在每个 json 文档之前添加标题行的最佳方法。 https://stackoverflow.com/a/30899000/5029432

【讨论】:

  • 很酷,但是请注意,该解决方案的一个缺点是它不允许指定文档 ID。
  • @Val,当你通过 BULK API 导入文件时,id 会自动生成。
  • 我知道,我只是为那些偶然发现这个问题并需要指定自己的 id 的人提到这一点。
猜你喜欢
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2018-02-06
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多