V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Zioyi
V2EX  ›  Elasticsearch

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

  •  
  •   Zioyi · 2017-09-07 14:28:38 +08:00 · 4732 次点击
    这是一个创建于 2395 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在 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)>

    3 条回复    2017-09-07 15:31:05 +08:00
    Zioyi
        1
    Zioyi  
    OP
       2017-09-07 14:51:57 +08:00
    更正:
    我的问题是,如何通过 logstash 将 Mysql 数据的两张表直接同步 ES 中的索引(索引中有两个 types,为父子关系),或者有没有其他的方法达到此目的
    Phant0m
        2
    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
        3
    Zioyi  
    OP
       2017-09-07 15:31:05 +08:00
    主要是 ip 类型和 ipa 类型之间有父子关系,这样写 ipa 表中的数据会找不到它的父
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4337 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:08 · PVG 18:08 · LAX 03:08 · JFK 06:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.