首页 > 解决方案 > 在 aws elasticsearch 中插入 json 文件时出错

问题描述

我有一个小的 json 文件在 elasticsearch 中使用 t2 实例,但我无法在 aws elasticsearch 中插入数据

我的文件如下所示

[{"Company_Name": "11 plc (NGSE:MOBIL)", "Business_Description": "11 plc markets petroleum products in Nigeria."}, {"Company_Name": "3Power Energy Group, Inc. (OTCPK:PSPW)", "Business_Description": "3Power Energy Group, Inc. focuses on developing, building, and operating power plants."}, {"Company_Name": "4Sight Holdings Limited (JSE:4SI)", "Business_Description": "4Sight Holdings Limited, through its subsidiaries, provides technology support solutions across various industries in Mauritius."}, {"Company_Name": "A'ayan Leasing and Investment Company K.S.C.P. (KWSE:AAYAN)", "Business_Description": "A'ayan Leasing and Investment Company K.S.C.P. engages in financial investments, trading and investing in properties"}]

我尝试使用以下命令

curl -s -XPOST https://elasticsearchdomain/_bulk?pretty -H "Content-Type: application/x-ndjson" --data-binary '@data.json'

我收到状态 400 错误

{
  "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
}

我究竟做错了什么?

标签: amazon-web-servicesamazon-elasticsearch

解决方案


Elastic Search 中的 Bulk API 并没有达到您的预期。批量 API 用于在一次 API 调用中执行多项操作(例如索引、更新、删除)。因此,您必须指定要执行的特定操作类型。在这种情况下,您要索引多个文档:

curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "myindex", "_id" : "1" } }
{"Company_Name": "11 plc (NGSE:MOBIL)", "Business_Description": "11 plc markets petroleum products in Nigeria."}
{ "index" : { "_index" : "myindex", "_id" : "2" } }
{"Company_Name": "3Power Energy Group, Inc. (OTCPK:PSPW)", "Business_Description": "3Power Energy Group, Inc. focuses on developing, building, and operating power plants."}
'

因此,对于您指定操作(在本例中为索引)的每个文档,通过换行符终止操作,然后再次指定文档(由换行符终止)。


推荐阅读