简体   繁体   中英

indexing mysql records with elastic search

I am trying to get resultset from mysql db and then converting to json and then indexing using elastic search api.

Did google and found that using logstash I can directly index the data to elastic search,searched a lot but dint find any sample code how to do this via java code.

I am not sure which will be the best way to go for (indexing using logstash or convert resultset into json and then do indexing via elasticsearch).

Any sample code/link will be highly helpful.

If you're willing to do it using logstash you might have to use the jdbc input plugin, where you may able to draw data from your MySQL database and index it into your elasticsearch instance.

input {
    jdbc {
        jdbc_connection_string => "jdbc:mysql://yourhost:3306/yourdb" 
        jdbc_user => "root"
        jdbc_password => "root"
        jdbc_validate_connection => true
        jdbc_driver_library => "/pathtojar/mysql-connector-java-5.1.39-bin.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        schedule => "* * * * *" <-- if you need the query to be running continuously at a time span 
        statement => "SELECT * FROM yourtable" <-- change the query to your need        
        jdbc_paging_enabled => "true"
        jdbc_page_size => "50000"
    }
}

The above is just a sample, so that you could reproduce it to your need. I'm not pretty sure of which way it's better, whether to converting it to json or plainly index it using logstash . You might want to have a look at this thread . Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM