Json通过API到Elasticsearch

埃洛伊

我正在尝试向Elasticsearch添加一个json文件,该文件大约有30.000行,并且格式不正确。我正在尝试通过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"
}

我确实知道Bulk API的格式需要{"index":{"_id":*}}在文件中的每个json对象之前,如下所示:

{"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中阅读了数百万篇帖子,但这是我的第一篇!#绝望

提前非常感谢您!

埃洛伊

感谢您提供的所有答案,它们确实帮助我朝正确的方向前进。

我制作了一个bash脚本来自动化日志的下载,格式化和上载到Elasticsearch:

#!/bin/bash

echo "Downloading logs from Sophos Central. Please wait."

cd /home/user/ELK/Sophos-Central-SIEM-Integration/log

#This deletes the last batch of results
rm result.json
cd .. 

#This triggers the script to download a new batch of logs from Sophos

./siem.py
cd /home/user/ELK/Sophos-Central-SIEM-Integration/log

#Adds newline at the beginning of the logs file
sed -i '1 i\{"index":{}}' result.json

#Adds indexes
sed -i '3~2s/^/{"index":{}}/' result.json

#Adds json file to elasticsearch 
curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/ivc/default/_bulk?pretty --data-binary @result.json

这就是我实现这一目标的方式。可能会有更简单的选择,但是这个对我有用。希望对其他人有用!

再次感谢大家!:D

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章