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

时间:2013-12-17 23:32:27

标签: json elasticsearch bigdata artificial-intelligence elasticsearch-plugin

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

9 个答案:

答案 0 :(得分: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"}

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

$ 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"}

jq' -c标志确保每个文档都在一行上。

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

答案 1 :(得分:18)

您应该使用Bulk 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}}]}

答案 2 :(得分:11)

我确定有人想要这样,这样我就可以轻松找到。

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

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);
    }
  });
}

希望我能帮助的不仅仅是我自己。不是火箭科学,但可以节省10分钟。

干杯

答案 3 :(得分: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

答案 4 :(得分:8)

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

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

Read more here : ES API

一个简单的shell脚本可以解决这个问题,如果你对shell这样的东西感觉很舒服(未经测试):

while read line
do
curl -XPOST 'http://localhost:9200/<indexname>/<typeofdoc>/' -d "$line"
done <myfile.json

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

pyes on github
elastic search python client

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

答案 5 :(得分:5)

Stream2es是IMO最简单的方式。

e.g。假设一个文件“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

答案 6 :(得分:4)

您可以使用esbulk,一个快速而简单的批量索引器:

$ esbulk -index myindex file.ldj

这是一个asciicast,显示它在大约11s内将Project Gutenberg数据加载到Elasticsearch中。

免责声明:我是作者。

答案 7 :(得分:3)

您可以使用Elasticsearch Gatherer插件

Elasticsearch的gatherer插件是一个可伸缩数据获取和索引的框架。内容适配器在gatherer zip存档中实现,这是一种可在Elasticsearch节点上分发的特殊插件。他们可以接收作业请求并在本地队列中执行它们。工作状态保存在特殊索引中。

此插件正在开发中。

里程碑1 - 将收集器拉链部署到节点

里程碑2 - 工作规范和执行

里程碑3 - 将JDBC河移植到JDBC收集器

里程碑4 - 按负载/队列长度/节点名称分配收集作业,cron作业

里程碑5 - 更多收集者,更多内容适配器

参考https://github.com/jprante/elasticsearch-gatherer

答案 8 :(得分:0)

一种方法是创建一个执行批量插入的bash脚本:

curl -XPOST http://127.0.0.1:9200/myindexname/type/_bulk?pretty=true --data-binary @myjsonfile.json

运行插入后,运行此命令以获取计数:

curl http://127.0.0.1:9200/myindexname/type/_count