[英]Elasticsearch: Bulk request throws error in Elasticsearch 6.1.1
我最近升級到 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 文件末尾添加空行並保存文件,然后嘗試運行以下命令
curl -XPOST localhost:9200/subscribers/ppl/_bulk?pretty --data-binary @customers_full.json -H 'Content-Type: application/json'
正如文檔所說:使用--data-binary
標志而不是普通的-d
-d
不保留換行符,也不格式化 json 。
由於JSON 格式,我遇到了這個問題。
錯誤很明顯:
The bulk request must be terminated by a newline [\n]
所以你只需要在你的customers_full.json
文件的末尾添加一個換行符就可以了。
我遇到了同樣的問題,並在有人指出我輸入錯誤的文件名之前花了幾個小時添加和刪除換行符......所以請注意,如果文件實際上不存在,curl 會拋出相同的錯誤,這使得這非常令人困惑。
我在使用Elasticsearch 7.3時遇到了類似的問題。
這是我解決它的方法。
.json
文件,比如products.json
文件。.json
文件。.json
文件的末尾,然后按鍵盤上的 ENTER 鍵。.json
文件。 這將在.json
文件的末尾創建一個新行。 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"
僅此而已。
我希望這會有所幫助
在 JSON 文件中的行尾按 Enter 並再次運行該命令。
curl -H "Content-Type: application/x-ndjson" -XPOST 'localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"generated.json"
對於任何使用郵遞員向 ElasticSearch 發出請求的人
只需按 Enter 即可創建一個空的新行!
瞧,問題解決了
您只需要打開 json 文件,然后轉到文件末尾(Ctrl+end),然后按 Enter 換行。
您需要在 curl 請求中使用--data-binary
而不是-d
。 請參閱: 批量 API
這對我有用:
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"
對我來說,問題只是由於錯誤的文件名。 我在命令中使用了 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"
當我在 CURL 命令下方使用時,我在使用彈性 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
我為此苦苦掙扎了一分鍾。 我的問題是由 curl 請求中--data
和-binary
之間的空格引起的,並給出了相同的錯誤 - must end with new line [\\\\n]}
。
所以仔細檢查 curl req 中的--data-binary
不是--data - binary
這在我的本地設置中有效。
curl -H "Content-type:application/json" -XPOST "http://localhost:9200/customer/personal/_bulk?pretty" --data-binary @"generated.json"
我只是忘記了文件名前的 add 和@
符號,就像這樣
--data-binary "@products.json"
如果您不使用數據文件,您會怎么做? 我遇到了問題,但沒有從文件發送數據。
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);
要檢查的幾件事:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.