代码之家  ›  专栏  ›  技术社区  ›  Kirzilla

单次插入可以,但批量导入会引发类型为“not_x_content_exception”的错误

  •  0
  • Kirzilla  · 技术社区  · 7 年前

    我正在尝试将数据从JSON文件导入Elasticsearch,该文件每行包含一个文档。只有数据。

    以下是我如何创建索引并尝试插入一个文档:

    DELETE /tests

    PUT /tests
    {}
    
    PUT /tests/test/_mapping
    {
      "test":{
        "properties":{
          "env":{"type":"keyword"},
          "uid":{"type":"keyword"},
          "ok":{"type":"boolean"}
        }
      }
    }
    
    POST /tests/test
    {"env":"dev", "uid":12346, "ok":true}
    
    GET /tests/_search
    {"query":{"match_all":{}}}
    

    一切正常,没有错误,文档索引正确,可以在ES中找到。

    现在让我们试着用 elasticdump .

    以下是我试图导入的文件内容:

    cat ./data.json
    {"env":"prod","uid":1111,"ok":true}
    {"env":"prod","uid":2222,"ok":true}
    
    

    以下是我尝试导入的方式:

    elasticdump \
        --input="./data.json" \
        --output="http://elk:9200" \
        --output-index="tests/test" \
        --debug \
        --limit=10000 \
        --headers='{"Content-Type": "application/json"}' \
        --type=data
    

    但我错了 Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes .

    以下是完整的输出:

    root@node-tools:/data# elasticdump \
    >     --input="./s.json" \
    >     --output="http://elk:9200" \
    >     --output-index="tests/test" \
    >     --debug \
    >     --limit=10000 \
    >     --headers='{"Content-Type": "application/json"}' \
    >     --type=data
    Tue, 16 Apr 2019 16:26:28 GMT | starting dump
    Tue, 16 Apr 2019 16:26:28 GMT | got 2 objects from source file (offset: 0)
    Tue, 16 Apr 2019 16:26:28 GMT [debug] | discovered elasticsearch output major version: 6
    Tue, 16 Apr 2019 16:26:28 GMT [debug] | thisUrl: http://elk:9200/tests/test/_bulk, payload.body: "{\"index\":{\"_index\":\"tests\",\"_type\":\"test\"}}\nundefined\n{\"index\":{\"_index\":\"tests\",\"_type\":\"test\"}}\nundefined\n"
    { _index: 'tests',
      _type: 'test',
      _id: 'ndj4JmoBindjidtNmyKf',
      status: 400,
      error:
       { type: 'mapper_parsing_exception',
         reason: 'failed to parse',
         caused_by:
          { type: 'not_x_content_exception',
            reason:
             'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes' } } }
    { _index: 'tests',
      _type: 'test',
      _id: 'ntj4JmoBindjidtNmyKf',
      status: 400,
      error:
       { type: 'mapper_parsing_exception',
         reason: 'failed to parse',
         caused_by:
          { type: 'not_x_content_exception',
            reason:
             'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes' } } }
    Tue, 16 Apr 2019 16:26:28 GMT | sent 2 objects to destination elasticsearch, wrote 0
    Tue, 16 Apr 2019 16:26:28 GMT | got 0 objects from source file (offset: 2)
    Tue, 16 Apr 2019 16:26:28 GMT | Total Writes: 0
    Tue, 16 Apr 2019 16:26:28 GMT | dump complete
    

    我做错了什么?为什么手动插入在 _batch 就是抛出错误。有什么想法吗?

    UPD

    尝试使用python的 elasticsearch_loader -很好。

    elasticsearch_loader \
        --es-host="http://elk:9200" \
        --index="tests" \
        --type="test" \
        json --json-lines ./data.json
    

    可以在此处找到一些其他信息: https://github.com/taskrabbit/elasticsearch-dump/issues/534

    0 回复  |  直到 7 年前
        1
  •  2
  •   Kirzilla    7 年前

    Json文档应按如下方式提供: _source .

    是: {"env":"prod","uid":1111,"ok":true}

    现在: {"_source":{"env":"prod","uid":1111,"ok":true}}

    这可以在飞机上完成 elasticdump 使用 --transform 论点:

    elasticdump \
        --input="./data.json" \
        --output="http://elk:9200" \
        --output-index="tests/test" \
        --debug \
        --limit=10000 \
        --type=data \
        --transform="doc._source=Object.assign({},doc)"
    

    感谢github的@ferronrsmith。 更多详情请点击此处: https://github.com/taskrabbit/elasticsearch-dump/issues/534