有没有办法在elasticsearch服务器中导入json文件(包含100个文档).

sha*_*hak 32 json artificial-intelligence bigdata elasticsearch elasticsearch-plugin

有没有办法在elasticsearch服务器中导入JSON文件(包含100个文档)?我想将一个大的json文件导入es-server ..

Pet*_*ter 39

正如dadoonet已经提到的那样,批量API可能就是这样.要转换批量协议的文件,可以使用jq.

假设文件只包含文件本身:

$ echo '{"foo":"bar"}{"baz":"qux"}' | 
jq -c '
{ index: { _index: "myindex", _type: "mytype" } },
. '

{"index":{"_index":"myindex","_type":"mytype"}}
{"foo":"bar"}
{"index":{"_index":"myindex","_type":"mytype"}}
{"baz":"qux"}
Run Code Online (Sandbox Code Playgroud)

如果文件包含顶级列表中的文档,则必须先将其解包:

$ echo '[{"foo":"bar"},{"baz":"qux"}]' | 
jq -c '
.[] |
{ index: { _index: "myindex", _type: "mytype" } },
. '

{"index":{"_index":"myindex","_type":"mytype"}}
{"foo":"bar"}
{"index":{"_index":"myindex","_type":"mytype"}}
{"baz":"qux"}
Run Code Online (Sandbox Code Playgroud)

jq的-c标志确保每个文档本身都在一行上.

如果你想直接卷曲,你会想要使用--data-binary @-,而不仅仅是-d,否则curl将再次剥离换行符.


dad*_*net 20

您应该使用批量API.请注意,您需要在每个json文档之前添加标题行.

$ cat requests
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
$ curl -s -XPOST localhost:9200/_bulk --data-binary @requests; echo
{"took":7,"items":[{"create":{"_index":"test","_type":"type1","_id":"1","_version":1,"ok":true}}]}
Run Code Online (Sandbox Code Playgroud)

  • 不是.如果你使用`index/type/_bulk`端点,你也可以忽略`_index`和`_type`. (3认同)
  • 只是为了获取信息(如果有人遇到这个问题),是的,它适用于一个空索引标题,由@The Red Pea编写 (2认同)

Der*_*yck 11

我确定有人想要这个,所以我会很容易找到.

仅供参考 - 这是在与全新ES实例相同的服务器上使用Node.js(基本上作为批处理脚本).将它放在2个文件中,每个文件包含4000个项目,在我的共享虚拟服务器上只花了大约12秒.因人而异

var elasticsearch = require('elasticsearch'),
    fs = require('fs'),
    pubs = JSON.parse(fs.readFileSync(__dirname + '/pubs.json')), // name of my first file to parse
    forms = JSON.parse(fs.readFileSync(__dirname + '/forms.json')); // and the second set
var client = new elasticsearch.Client({  // default is fine for me, change as you see fit
  host: 'localhost:9200',
  log: 'trace'
});

for (var i = 0; i < pubs.length; i++ ) {
  client.create({
    index: "epubs", // name your index
    type: "pub", // describe the data thats getting created
    id: i, // increment ID every iteration - I already sorted mine but not a requirement
    body: pubs[i] // *** THIS ASSUMES YOUR DATA FILE IS FORMATTED LIKE SO: [{prop: val, prop2: val2}, {prop:...}, {prop:...}] - I converted mine from a CSV so pubs[i] is the current object {prop:..., prop2:...}
  }, function(error, response) {
    if (error) {
      console.error(error);
      return;
    }
    else {
    console.log(response);  //  I don't recommend this but I like having my console flooded with stuff.  It looks cool.  Like I'm compiling a kernel really fast.
    }
  });
}

for (var a = 0; a < forms.length; a++ ) {  // Same stuff here, just slight changes in type and variables
  client.create({
    index: "epubs",
    type: "form",
    id: a,
    body: forms[a]
  }, function(error, response) {
    if (error) {
      console.error(error);
      return;
    }
    else {
    console.log(response);
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

希望我能帮助更多,而不仅仅是我自己.不是火箭科学,但可能会节省10分钟.

干杯


max*_*max 10

jq是一个轻量级且灵活的命令行JSON处理器.

用法:

cat file.json | jq -c '.[] | {"index": {"_index": "bookmarks", "_type": "bookmark", "_id": .id}}, .' | curl -XPOST localhost:9200/_bulk --data-binary @-

我们正在使用文件file.json并使用-c标志将其内容管道传输到jq以构造压缩输出.这是块金:我们利用了jq每行输入不仅可以构造一个而且构造多个对象这一事实.对于每一行,我们创建控件JSON Elasticsearch需要(使用我们原始对象的ID)并创建第二行,它只是我们原始的JSON对象(.).

在这一点上,我们将JSON格式化为Elasticsearch的批量API所期望的方式,因此我们只需将其管道卷曲,将其发送到Elasticsearch!

归功于Kevin Marsh


mco*_*lin 8

导入否,但您可以使用ES API索引文档.

您可以使用索引api加载每一行(使用某种代码来读取文件并进行curl调用)或索引bulk api来加载它们.假设您的数据文件可以格式化以使用它.

在这里阅读更多:ES API

一个简单的shell脚本可以解决这个问题,如果你觉得这样的shell可能(未经测试):

while read line
do
curl -XPOST 'http://localhost:9200/<indexname>/<typeofdoc>/' -d "$line"
done <myfile.json
Run Code Online (Sandbox Code Playgroud)

在Peronally,我可能会使用Python pyes或弹性搜索客户端.

在github 弹性搜索python客户端上的pyes

Stream2es对于快速将数据加载到es中也非常有用,并且可能有一种方法可以简单地流式传输文件.(我还没有测试过一个文件但是用它来加载维基百科文档以进行es perf测试)


Jon*_*ess 5

Stream2es是IMO最简单的方式.

例如,假设一个文件"some.json"包含一个JSON文档列表,每行一个:

curl -O download.elasticsearch.org/stream2es/stream2es; chmod +x stream2es
cat some.json | ./stream2es stdin --target "http://localhost:9200/my_index/my_type
Run Code Online (Sandbox Code Playgroud)