2017-04-01 102 views
0

我正试图将所有的json导入到我的elasticsearch中。为此,我尝试了curl命令,但没有用,因为它开始给出解析错误。
这是我的尝试:如何使用Logstash或curl将数据提供给ElasticSearch?

curl -XPOST "http://localhost:9200/test" -d "@test.json" 
curl -XPOST "http://localhost:9200/test/_bulk" -d "@test.json" 
curl -XPOST "http://localhost:9200/test/_bulk" --data-binary "@test.json" 
curl -s -XPOST "http://localhost:9200/test/_bulk" --data-binary "@test.json" 

等诸多尝试。但我得到的是Parsing error。因此想知道弹性搜索我的所有记录的方法是什么?
此外,如果有人可以帮助我用logstash解决它,那么它将是一个很大的帮助。请让我知道最好的建议。
以下是JSON中的示例数据,它可能用换行符分隔。 Sample Data
以下是错误:

{ 
    "error" : { 
    "root_cause" : [ 
     { 
     "type" : "parse_exception", 
     "reason" : "Failed to derive xcontent" 
     } 
    ], 
    "type" : "parse_exception", 
    "reason" : "Failed to derive xcontent" 
    }, 
    "status" : 400 
} 

回答

2

你需要改变你的JSON这样的:

{ "index" : { "_index" : "test", "_type" : "type" } } 
{"data":...} 
{ "index" : { "_index" : "test", "_type" : "type" } } 
{"data":...} 
{ "index" : { "_index" : "test", "_type" : "type" } } 
{"data":...} 

然后你可以运行:

curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@test.json" 

了解更多关于Bulk API

如果您希望使用Logstash做到这一点,您可以使用stdin inputjson filter,然后使用elasticsearch output。喜欢的东西(未测试):

input { 
    stdin { } 
} 

filter { 
    json { 
    source => "message" 
    } 

    mutate { 
    remove_field => [ "message" ] 
    } 
} 

output { 
    elasticsearch { 
    } 
} 

然后启动:

cat test.json | bin/logstash -f logstash.conf 

我希望这有助于。

+0

没有logstash会这样做 – dadoonet

+0

顺便说一句,如果你将文件分割成多个文件,比如每个文件一个JSON文件,你可以使用https://github.com/dadoonet/fscrawler项目。 – dadoonet

+0

有这个选项:https://github.com/dadoonet/fscrawler#indexing-json-docs – dadoonet

相关问题