关于 ElasticSearch 和 Mysql 数据同步的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Zioyi
V2EX    Elasticsearch
  •  
  •   Zioyi 2017-09-07 14:28:38 +08:00 5436 次点击
    这是一个创建于 2988 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在 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     Solana     1330 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 17:08 PVG 01:08 LAX 09:08 JFK 12:08
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86