这个问题来自于this SO thread。
正如@Val所建议的那样,我似乎有一个类似但不相同的查询,最好是让其他人有一个单独的问题,以便从中受益。
因此,与上述类似,我需要在索引中插入大量数据(我最初的测试是大约10 000个文档,但这只是针对POC,还有更多的文档)。我想插入的数据在一个.json文档中,看起来如下(片段):
[ { "fileName": "filename", "data":"massive string text data here" },
{ "fileName": "filename2", "data":"massive string text data here" } ]尽管我自己承认我是ElasticSearch的新手,但是,通过阅读文档,我的假设是我可以获取一个.json文件,并从内部的数据创建一个索引。从那以后,我了解到,在json中的每一项似乎都需要一个“标题”,类似于:
{"index":{}}
{ "fileName": "filename", "data":"massive string text data here" }这意味着,这不是实际的json格式(如此),而是操纵字符串。
我想知道是否有一种方法可以按原样(以json格式)来import我的json数据,而不必首先手动操作文本(因为我的测试数据有10000个条目,我相信您可以理解为什么我不喜欢手动操作)。
有什么建议或建议的自动化工具来帮助这一点吗?
PS -我正在使用窗口安装程序和邮递员的电话。
发布于 2017-08-10 04:28:55
您可以使用这样的单个shell命令很容易地转换您的文件。如果您的文件名为input.json,则可以这样做:
jq -c -r ".[]" input.json | while read line; do echo '{"index":{}}'; echo $line; done > bulk.json在此之后,您将有一个名为bulk.json的文件,该文件的格式是正确的,以便发送到大容量端点。
然后,您可以像这样调用大容量端点:
curl -XPOST localhost:9200/your_index/your_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary @bulk.json注意:如果您还没有jq,您需要先使用它。
发布于 2021-06-27 14:31:29
这是我的代码,可以将数据批量到es。
const es = require("elasticsearch");
const client = new es.Client({
hosts: ["http://localhost:9200"],
});
const cities = <path to your json file>;
let bulk: any = [];
cities.forEach((city: any) => {
bulk.push({
index: {
_index: <index name>,
_type: <type name>,
},
});
bulk.push(city);
});
//loop through each city and create and push two objects into the array in each loop
//first object sends the index and type you will be saving the data as
//second object is the data you want to index
client.bulk({ body: bulk }, function (err: any, response: any) {
if (err) {
console.log("Failed Bulk operation", err);
} else {
console.log("Successfully imported %s", cities.length);
}
});或者您可以使用库,如弹性转储或弹力搜索-工具
https://stackoverflow.com/questions/45601344
复制相似问题