如何将 JSON 文件导入 ElasticSearch 6.x?

How to import JSON file to ElasticSearch 6.x?

我在我的 CentOS7 主机的 9200 端口上安装了 Elastic Search 6.1.1 运行 的全新空安装。我有这个 "es.json" 文件,其中包含准备插入 ES 的数据样本。

我还有一个日志文件,其中每一行都是一个 json 块。我可以同时使用它们来填充我的 ES 数据库。

如何将此数据和其他数据插入 ES?

我没有在文档和互联网上找到很好的解释。由于某种原因,文档没有说得很清楚

使用 curl 中的 --data-binary 标志从 JSON 文件批量导入。

curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/{index}/{type}/_bulk?pretty' --data-binary @es.json

数据可以发布到端点之一 - /_bulk/{index}/_bulk{index}/{type}/_bulk。当提供 {index}{index}/{type} 时,它们将默认用于未明确提供它们的批量项目。

Content-Type: application/x-ndjson 代表换行分隔 JSON.

在导入 JSON 文件之前,您可能希望 define mappings yourself or let Elasticsearch generate mappings dynamically during import. If you don't want Elasticsearch to generate mappings dynamically during import, refer to this doc 自己定义映射。

参考文献:

您可以使用 elasticsearch_loader 将 json 个文件加载到 elasticsearch(2.X、5.X、6.X)。

可以用pip下载:

pip install elasticsearch-loader

然后您将能够通过发出以下命令将 json 个文件加载到 elasticsearch 中:

elasticsearch_loader --index incidents --type incident json file1.json file2.json

免责声明:我是 elasticsearch_loader

的作者