关于 ElasticSearch 和 Mysql 数据同步的问题

2017-09-07 14:28:38 +08:00
 Zioyi

我在 mysql 中有两张表 ip 和 ipa

ip 表: |ipid | values          |
          1    192.168.168.168
ipa 表: |ipaid| city | ipid
                 HF      1
即 ipa 表的外键 ipid 与 ip 表的 ipid 对应

我现在想把这两张表的数据同步到 ElasticSearch (简称 ES ),为了关联查询,我首先在 ES 中定义了父子映射:

curl -XPUT "http://localhost:9200/ti?pretty" -d '
{
  "mappings": { 
    "ip" : { }, 
    "ipa":{ 
      "_parent": { 
        "type": "ip"}}
}

在此基础上,我通过 logstash 的 Jdbc input plugin 来实现实时的数据同步,关于 jdbc.conf 的编写:

input {
    stdin {
    }
    jdbc {
      # mysql jdbc connection string to our backup databse
      jdbc_connection_string => "jdbc:mysql://192.168.162.1:3306/model_test"
      # the user we wish to excute our statement as
      jdbc_user => "root"
      jdbc_password => "123"
      # the path to our downloaded jdbc driver
      jdbc_driver_library => "mysql-connector-java-5.1.43.jar"
      # the name of the driver class for mysql
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      jdbc_paging_enabled => "true"
      jdbc_page_size => "50000"
      statement_filepath => "jdbc.sql"
      schedule => "* * * * *"
      type => "jdbc_ip"
    }
     jdbc {
      # mysql jdbc connection string to our backup databse
       jdbc_connection_string => "jdbc:mysql://192.168.162.1:3306/model_test"
      # the user we wish to excute our statement as
      jdbc_user => "root"
      jdbc_password => "123"
      # the path to our downloaded jdbc driver
      jdbc_driver_library => "mysql-connector-java-5.1.43.jar"
      # the name of the driver class for mysql
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      jdbc_paging_enabled => "true"
      jdbc_page_size => "50000"
      statement_filepath => "jdbc2.sql"
      schedule => "* * * * *"
      type => "jdbc_ip_address"
    }
}
filter {
    json {
        source => "message"
        remove_field => ["message"]
    }
}     
output {
 stdout {
        codec => json_lines
    }
    if[type] == "jdbc_ip"{
        elasticsearch {
        hosts  => "localhost:9200"
        index => "ti"
        document_type => "ip"
        document_id => "%{id}"
        }
    }
    if[type] == "jdbc_ip_address"{
        elasticsearch {
        hosts  => "localhost:9200"
        index => "ti"
        document_type => "ipa"
        document_id => "%{id}"
        ## document_parent => "%{ip_id}" ##此处错误
        }
    }
}

我上面的配置文件无法实现:将 Mysql 中的 ip 和 ipa 两张表直接同步到我在 ES 中已建好的 ti 索引
我的问题是,如何通过 logstash 将 Mysql 数据的两张表直接同步 ES 中的索引(索引中有两个文档,为父子关系),或者有没有其他的方法达到此目的。
<附加:ElasticSearch ( 5.2.2 )、logstash(5.5.2)、环境( centos6.5)>

4772 次点击
所在节点    Elasticsearch
3 条回复
Zioyi
2017-09-07 14:51:57 +08:00
更正:
我的问题是,如何通过 logstash 将 Mysql 数据的两张表直接同步 ES 中的索引(索引中有两个 types,为父子关系),或者有没有其他的方法达到此目的
Phant0m
2017-09-07 15:12:17 +08:00
```
input {
jdbc {
jdbc_driver_library => "mysql-connector-java-5.1.43.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://192.168.162.1:3306/model_test"
jdbc_user => "root"
jdbc_password => "123"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
statement => "sql 语句/ sql 文件"
type => "jdbc_ip"
}
jdbc {
jdbc_driver_library => "mysql-connector-java-5.1.43.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://192.168.162.1:3306/model_test"
jdbc_user => "root"
jdbc_password => "123"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
statement => "sql 语句/ sql 文件"
type => "jdbc_ip_address"
}
}
output {
stdout {
codec => rubydebug
}
if[type] == "jdbc_ip"{
elasticsearch {
hosts => "localhost:9200"
index => "ti"
document_type => "ip"
document_id => "%{id}"
}
}
if[type] == "jdbc_ip_address"{
elasticsearch {
hosts => "localhost:9200"
index => "ti"
document_type => "ipa"
document_id => "%{id}"
}
}
}
```
或者你先把 output 里的 elasticsearch 都注释掉,只留 stdout 看看是否能正常输出
Zioyi
2017-09-07 15:31:05 +08:00
主要是 ip 类型和 ipa 类型之间有父子关系,这样写 ipa 表中的数据会找不到它的父

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/388910

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX