MySQL数据多个表导入到elasticsearch

大多数情况下我们的数据都存放在了数据库中,
但是elasticsearch它有自己的索引库,那么如果我们在做搜索的是时候就需要将数据库中的数据同步到elasticsearch中
在这里我们使用logstash的logstash-jdbc-input的插件进行与数据库的同步,对于logstash与数据库同步,我们可以设置elasticsearch与数据库同步的时间,使用这种方式进行同步还是很方便的。

先下载logstash
这个就官网下载就行了

然后下载驱动
这个在MySQL官网就可以下载
mysql-connector-java-8.0.17.jar
重点是写logstash配置文件

多个表 对应 多个索引 多个jdbc 多个type

input {
    jdbc {
        jdbc_connection_string => "jdbc:mysql://localhost:3306/ops_admin"
        jdbc_user => "账号"
        jdbc_password => "密码"
        jdbc_driver_library => "/usr/local/drive/mysql-connector-java-8.0.17.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_paging_enabled => "true" # 这个地方管是否分页的
        jdbc_page_size => "50000" # 一次分多少 ,太大的必须分页
        statement => "select * from 表名"
        schedule => "* * * * *"
        type => "type1"
    }
     jdbc {
        jdbc_connection_string => "jdbc:mysql://localhost:3306/ops_admin"
        jdbc_user => "xxx"
        jdbc_password => "xxx"
        jdbc_driver_library => "/usr/local/drive/mysql-connector-java-8.0.17.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_paging_enabled => "true"
        jdbc_page_size => "50000"
        statement => "select * from 表名"
        schedule => "* * * * *"
        type => "type2"
    }
}
output {
    if[type] == "type1" {
        elasticsearch {
            hosts => ["172.25.16.240:9200"]
            index => "index_索引名"
            document_id => "%{id}"
        }
    }
    if[type] == "type2" {
        elasticsearch {
            hosts => ["172.25.16.240:9200"]
            index => "index_索引名"
            document_id => "%{id}"
        }
    }
}

这个玩意是MySQL表里的ID 这个字段 自增不重复
得指定这个 后来我才发现的,没这个玩意,到es里面了 数据重复了

document_id => "%{id}"

然后就可以启动他了

nohup bin/logstash -f config/mysql.conf

还有一点 他也可以是sql文件 ,但是这个地方要改成

statement_filepath => "jdbc.sql"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值