【问题标题】:Elasticsearch: Bulk request throws error in Elasticsearch 6.1.1Elasticsearch:批量请求在 Elasticsearch 6.1.1 中引发错误
【发布时间】:2023-04-03 09:39:01
【问题描述】:

我最近升级到 Elasticsearch 6.1.1 版,现在我无法从 JSON 文件批量索引文档。当我内联时,它工作正常。以下是文档内容:

{"index" : {}}
{"name": "Carlson Barnes", "age": 34}
{"index":{}}
{"name": "Sheppard Stein","age": 39}
{"index":{}}
{"name": "Nixon Singleton","age": 36}
{"index":{}}
{"name": "Sharron Sosa","age": 33}
{"index":{}}
{"name": "Kendra Cabrera","age": 24}
{"index":{}}
{"name": "Young Robinson","age": 20}

当我运行这个命令时,

curl -XPUT 'localhost:9200/subscribers/ppl/_bulk?pretty' -H 'Content-Type: application/json' -d @customers_full.json

我收到此错误:

"error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "The bulk request must be terminated by a newline [\n]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "The bulk request must be terminated by a newline [\n]"
  },
  "status" : 400

如果我在 Elasticsearch 5.x 中内联发送数据,它可以正常工作。我尝试在文件末尾添加换行符和换行符。好像没用。

【问题讨论】:

    标签: json elasticsearch elasticsearch-5 bulk-load


    【解决方案1】:

    错误很明显:

    The bulk request must be terminated by a newline [\n]
    

    因此,您只需在 customers_full.json 文件的末尾添加一个换行符就可以了。

    【讨论】:

    • 我做到了。我添加了一个换行符。我尝试添加换行符,仍然不起作用。
    • 我在 ES 6.1.3 上尝试了同样的方法,效果很好
    • 你应该尝试使用--data-binary而不是-d
    • 好吧,不知何故,您的文件末尾缺少一个明确的新行,我们应该看到一个完整的空行作为文档的最后一行
    【解决方案2】:

    你只需要打开json文件,然后转到文件末尾(Ctrl+end),然后请回车换行。

    【讨论】:

      【解决方案3】:

      您需要在 curl 请求中使用 --data-binary 而不是 -d。请看:Bulk API

      【讨论】:

        【解决方案4】:

        在 JSON 文件末尾添加 empty 行并保存文件,然后尝试运行以下命令

        curl -XPOST localhost:9200/subscribers/ppl/_bulk?pretty --data-binary @customers_full.json -H 'Content-Type: application/json'
        

        【讨论】:

        • 它是如此简单,我试图添加一个换行符来命令自己:| ..而它清楚地说'请求必须由新行终止'(y)
        • 我添加了新行,但问题仍然存在
        • 当我在 json 中添加了一个换行符 + 使用了 --data-binary 参数而不是 -d--data 时,它起作用了。单独添加换行符没有帮助。
        【解决方案5】:

        在有人指出我输入错误的文件名之前,我遇到了同样的问题并花了几个小时添加和删除换行符......所以请注意,如果文件实际上不存在,curl 会抛出相同的错误,这使得这个超级-令人困惑。

        【讨论】:

          【解决方案6】:

          正如文件所说: 使用--data-binary 标志而不是普通的-d

          -d 不保留换行符并且不格式化 json

          由于 JSON 格式,我遇到了这个问题。

          【讨论】:

          • 这个答案真的需要投票才能登上榜首。 --data-binary 确实与 -d 不同,即使在 Mac 上也是如此。我原以为二进制模式只会在 Windows 平台上有所作为!
          【解决方案7】:

          在 JSON 文件的行尾按 Enter 并再次运行命令。

          curl -H "Content-Type: application/x-ndjson" -XPOST 'localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"generated.json"
          

          【讨论】:

            【解决方案8】:

            这在我的本地设置中有效。

            curl -H "Content-type:application/json" -XPOST "http://localhost:9200/customer/personal/_bulk?pretty" --data-binary @"generated.json"
            

            【讨论】:

              【解决方案9】:

              对于任何使用邮递员向 ElasticSearch 提出请求的人

              只需按 Enter 即可创建一个空的新行!

              瞧,问题解决了

              【讨论】:

                【解决方案10】:

                这对我有用:

                curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@C:\Program Files\Elastic\Elasticsearch\7.2.0\accounts.json"
                

                【讨论】:

                  【解决方案11】:

                  我在使用 Elasticsearch 7.3 时遇到了类似的问题。

                  我是这样解决的。

                  1. 找到.json 文件,比如products.json 文件。
                  2. 双击以在文本编辑器中打开 .json 文件。
                  3. 滚动到 .json 文件的末尾,然后按键盘上的 ENTER 键。
                  4. 关闭.json 文件。这将在.json 文件的末尾创建一个新行。
                  5. 返回终端并运行以下命令。

                  N/B:对于下面的命令,.json 文件名是products.json,我将其导入到http://localhost:9200/ecommerce/product

                  curl -H "Content-type: application/json" -XPOST "http://localhost:9200/ecommerce/product/_bulk?pretty" --data-binary "@products.json"
                  

                  就是这样。

                  我希望这会有所帮助

                  【讨论】:

                    【解决方案12】:

                    我在 Windows 10 上使用 ElasticSearch 7.5.1 运行时遇到了同样的问题。

                    我尝试了所有答案;他们都没有工作。我确定我的文件末尾有一个换行符。

                    为了让它工作,我必须确保我上传的文件使用的是 UNIX 行尾字符(只有 0A,没有 0D),而且编码必须是 UTF-8。

                    使用 Notepad++,可以编辑文件的元数据。

                    终于有个好消息了:

                    【讨论】:

                      【解决方案13】:

                      对我来说,问题只是由于文件名错误。 我在命令中使用了 customer_full.json,而文件在我的文件系统中被命名为 customer_full(没有扩展名)。

                      所以在我的情况下,这个命令对我有用:

                      curl -H "Content-Type: application/x-ndjson" -XPOST 'http://localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"customer_full" 
                      

                      【讨论】:

                        【解决方案14】:

                        我只是忘记了像这样的文件名之前的 add 和 @ 符号

                        --data-binary "@products.json"
                        

                        【讨论】:

                          【解决方案15】:

                          当我使用下面的 CURL 命令时,我在使用 elastic 7.9.1 的 Windows 上遇到了类似的问题。

                          curl -s -H "Content-Type: application/json" -XPOST localhost:9200/accounts/docs/_bulk?filter_path=items.*.error --data-binary  "@textoutES.json"  >> erroredAtES.json"
                          

                          我尝试在文件末尾手动添加换行符,但没有成功。

                          我通过从 MySQL 数据库中提取数据来创建我的 JSON,如下所示,以确保我的记录应该以 LINE FEED 和 CARRIAGE RETURN 结尾。

                          然后,它对我有用:

                          SELECT CONCAT('{"index":{"_id":"',id,'"}}\r\n',request_data,'\r\n') reqestData FROM cards 
                          

                          更重要的是,如果您使用 Windows,则文件结尾应该有回车和换行 (CRLF)。此外,如果 JSON 中的任何行包含 CR 但没有 LF,那么您将得到解析异常 Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@2d5ed2ca

                          Windows CRLF and EOF

                          【讨论】:

                            【解决方案16】:

                            我为此苦苦挣扎了一分钟。我的错误是由我的 curl 请求中 --data-binary 之间的空格引起的,并给出了同样的错误 - must end with new line [\\n]}

                            所以仔细检查一下 curl req 中的 --data-binary 不是 --data - binary

                            【讨论】:

                              【解决方案17】:

                              如果你不使用数据文件,你会怎么做?我遇到了问题,但没有从文件发送数据。

                              const data1 = {
                                  "amount" : "100",
                                  "@timestamp" : `${UTC_timestamp}`,
                                  "transaction_attributes" : {
                                  "channel" : "channel-foobarbaz",
                                  "session_id" : "session-1234",
                                  "information" : "iinformation-foobarbaznformation-foobarbaz"
                                  },
                                  "currency" : {
                                  "currency_description" : "my currency description",
                                  },
                                  "external_timestamp" : "2021-12-03T11:22:55.206229500Z" };
                              
                                
                              // execute a post
                              let res = http.post(url,JSON.stringify(data1),params);
                              

                              【讨论】:

                              • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
                              猜你喜欢
                              • 1970-01-01
                              • 2020-07-24
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2012-12-09
                              • 2018-10-15
                              • 2016-04-16
                              • 2015-08-07
                              相关资源
                              最近更新 更多