【问题标题】:How to bulk change JSON format for Elastic Search如何批量更改 Elastic Search 的 JSON 格式
【发布时间】:2018-03-29 10:16:16
【问题描述】:

我有以下格式,并希望使用 elasticsearch 进行批量预处理。

{"title":"April","url":"https://simple.wikipedia.org/wiki/April", "abstract":"April is the 4th month of the year, and comes between March and May. It is one of four months to have 30 days.","sections":["The Month","April in poetry","Events in April","Fixed Events","Moveable Events","Selection of Historical Events","Trivia","References"]}
{"title":"August","url":"https://simple.wikipedia.org/wiki/August", "abstract":"August (Aug.) is the 8th month of the year in the Gregorian calendar, coming between July and September.","sections":["The Month","August observances","Fixed observances and events","Moveable and Monthlong events","Selection of Historical Events","Trivia","References"]}

我正在尝试在我的每一行之前添加索引、类型行。

{"index":{"_index":"myindex","_type":"wiki","_id":"1"}}

在阅读之前的帖子时,我使用的是Kevin Marsh's post,如下所示:

cat file.json jq -c '.[] | {"index": {"_index": "myindex", "_type": "wiki", "_id": .id}}, .' 

我没有使用管道,因为我试图找出之前的错误。我收到错误 jq:no such file or directory。然后我使用了jq --version and get jq-1.5-1-a5b5cbe

非常感谢任何帮助。

【问题讨论】:

  • 不确定您需要什么。
  • 感谢 Hatim,希望能找到我的 cat 语句中是否存在问题,或者找到更好的解决方案来更改格式,以便使用弹性搜索为批量 api 做好准备。
  • 据我了解,您有一个 json 文件,您想使用 Bulk API 在 Elasticsearch 中对其进行索引。对吗?
  • 是的 Hatim 及其超过 10000 行,如上,我想通过 addin {"index":{"_index":"myindex","_type":"wiki", "_id":"1"}} 在 json 文件中的每个对象之前。
  • 您需要正确格式化您的问题,它非常无组织

标签: json elasticsearch jq elasticsearch-bulk-api


【解决方案1】:

给你。这对我有用。让我知道这是否有帮助。

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

详细了解jq : a lightweight and flexible command-line JSON processor

【讨论】:

  • 未来绝对会格式化。对于那个很抱歉。 Hatim 以您提供的格式使用了以下命令:
  • 猫数据.json | jq -c'。 | {"index": {"_index": "json", "_type": "json"}}, .' | curl -XPOST localhost:9200/_bulk --data-binary @- 我收到以下错误: jq :error :syntax error , unexpected $end (unix shell quoting issues) at , line 1
  • 忘记卷曲部分。试试这个 "cat data.json | jq -c '。| {"index": {"_index": "json", "_type": "json"}}, .' " 看看你得到了什么输出。
  • Hatim - 你是个天才!你让我思考我做错了什么,我保留了原始格式并运行了前 10 行,它成功地检索了它们。我去了,也创建了索引。我现在正在做的是让命令针对我拥有的 1000 奇数行运行。它似乎正在缓慢地处理它们...将发布并让您知道...感谢您的所有帮助...
  • Hmmm ,命令处理大约 20K 行后出现错误。错误是: curl:(56) Recv failure : Connection reset by peer
【解决方案2】:

我们发现有必要在curl header中指定Content-Type;建议的解决方案应采用以下形式:

cat data.json | jq -c '. | {"index": {"_index": "json", "_type": "json"}}, .' | curl -H "Content-Type: application/json" -XPOST localhost:9200/_bulk --data-binary @-

【讨论】:

    猜你喜欢
    • 2015-02-07
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多