【发布时间】:2019-01-08 18:30:07
【问题描述】:
我正在尝试将一个 json 文件添加到具有大约 30.000 行并且格式不正确的 elasticsearch。我正在尝试通过 Bulk API 上传它,但我找不到正确格式化它的方法,它确实有效。我正在使用 Ubuntu 16.04LTS。
这是json的格式:
{
"rt": "2018-11-20T12:57:32.292Z",
"source_info": { "ip": "0.0.60.50" },
"end": "2018-11-20T12:57:32.284Z",
"severity": "low",
"duid": "5b8d0a48ba59941314e8a97f",
"dhost": "004678",
"endpoint_type": "computer",
"endpoint_id": "8e7e2806-eaee-9436-6ab5-078361576290",
"suser": "Katerina",
"group": "PERIPHERALS",
"customer_id": "a263f4c8-942f-d4f4-5938-7c37013c03be",
"type": "Event::Endpoint::Device::AlertedOnly",
"id": "83d63d48-f040-2485-49b9-b4ff2ac4fad4",
"name": "Peripheral allowed: Samsung Galaxy S7 edge"
}
我知道批量 API 的格式在文件中的每个 json 对象之前需要{"index":{"_id":*}},它看起来像这样:
{"index":{"_id":1}}
{
"rt": "2018-11-20T12:57:32.292Z",
"source_info": { "ip": "0.0.60.50" },
"end": "2018-11-20T12:57:32.284Z",
"severity": "low",
"duid": "5b8d0a48ba59941314e8a97f",
"dhost": "004678",
"endpoint_type": "computer",
"endpoint_id": "8e7e2806-eaee-9436-6ab5-078361576290",
"suser": "Katerina",
"group": "PERIPHERALS",
"customer_id": "a263f4c8-942f-d4f4-5938-7c37013c03be",
"type": "Event::Endpoint::Device::AlertedOnly",
"id": "83d63d48-f040-2485-49b9-b4ff2ac4fad4",
"name": "Peripheral allowed: Samsung Galaxy S7 edge"
}
如果我手动插入索引 id,然后使用这个表达式 curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:92100/ivc/default/bulk?pretty --data-binary @results.json 它将上传它而不会出错。
我的问题是,如何将索引 ID {"index":{"_id":*}} 添加到 json 的每一行以使其准备好上传?显然索引 id 必须每行添加 +1,有没有办法从 CLI 中做到这一点?
对不起,如果这篇文章看起来不正常,我在 Stack Overflow 上阅读了数百万篇文章,但这是我的第一篇! #绝望
提前非常感谢您!
【问题讨论】:
-
这个答案可能会有所帮助:stackoverflow.com/a/45604500/4604579
-
不知道你能不能用 CLI 做到这一点,但看看 logstash,应该很快。
-
谢谢你们!我要试试 Val 的解决方案!
-
不幸的是,这个带有 jq 的解决方案不起作用。 :( 它会在每个 json 对象的每个字段之后放置一个“索引”。我希望在每个整个 json 对象之后都有一个“索引”,因此 Bulk API 会接受它。显然它不采用这种格式。:/
标签: json elasticsearch