【问题标题】:How to upload mysql data to Elasticsearch如何将mysql数据上传到Elasticsearch
【发布时间】:2016-04-28 03:43:36
【问题描述】:

我是 Elasticsearch 的新手。

我正在尝试将我现有的 MySql 数据上传到 Elasticsearch。 Elasticsearch 批量导入使用 json 作为数据格式。这就是我将数据转换为 json 格式的原因。

employee.json:

[{"EmpId":"101", "Name":"John Doe", "Dept":"IT"}
 {"EmpId":"102", "Name":"FooBar", "Dept":"HR"}]

但我无法使用以下 curl 命令上传数据:

post: curl -XPOST 'localhost:9200/_bulk?pretty' --data-binary @employee.json

我收到一条解析异常消息。

看了一篇文档(https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html),我意识到数据格式应该是这样的:

action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n

我仍然不确定如何将我的数据格式化为上述格式并执行上传操作。

基本上我想知道 Elasticsearch 批量上传所期望的确切数据格式。也想知道我的curl命令是否正确。

【问题讨论】:

标签: elasticsearch


【解决方案1】:

您的数据应采用以下格式:

// if you want to use emp id as doc id specify otherwise dont add _id part
{ "index" : { "_index" : "index_name", "_type" : "type_name", "_id" : "101" } }
{"EmpId":"101", "Name":"John Doe", "Dept":"IT"}
{ "index" : { "_index" : "index_name", "_type" : "type_name", "_id" : "102" } }
{"EmpId":"102", "Name":"FooBar", "Dept":"HR"}
....

或者你可以使用logstash:https://www.elastic.co/blog/logstash-jdbc-input-plugin

【讨论】:

    【解决方案2】:

    来自文档:

    { "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
    { "field1" : "value1" }
    { "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
    { "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
    { "field1" : "value3" }
    { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
    { "doc" : {"field2" : "value2"} }
    

    所以您可能希望您的文件读取类似的内容

    { "update" : {"_id" : "101", "_type" : "foo", "_index" : "bar"} }
    {"EmpId":"101", "Name":"John Doe", "Dept":"IT"}
    

    https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-25
      • 2015-12-03
      • 2022-09-28
      • 2016-01-06
      • 1970-01-01
      • 2021-05-08
      • 2020-02-18
      • 2015-05-25
      相关资源
      最近更新 更多