# How to load tables from mysql to elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275>\
**Category:** Logstash\
**Created:** [July 23, 2020, 5:02am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275 "2020-07-23T05:02:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krishna\_Sai\_Nag\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krishna_sai_nag_g/32/80467_2.png) [@Krishna\_Sai\_Nag\_G](https://discuss.elastic.co/u/Krishna_Sai_Nag_G)\
**Post date:** [July 23, 2020, 5:02am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/1 "2020-07-23T05:02:25Z")

</div>

Hi,  
I want load multiple tables from mysql to different indexes in elastic search(i.e; each table in mysql to different index in elastic search), Is it possible to load using single logstash config file.  
Anyone can help on this.

---

<div class="post-metadata">

**Author:** ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)\
**Post date:** [July 23, 2020, 5:42am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/2 "2020-07-23T05:42:24Z")

</div>

If you want to do it in one pipeline, you could set up multiple JDBC inputs and add a metadata field (with `add_field`) to each one that says which index the data should go to and use that field in your Elasticsearch output with `index =>"%{[@metadata][index_name]}"`.  
If the index name depends not just on the table, but on the data of the individual entries (e.g. the log time) you could save the table name in the field and then use conditions like `if([@metadata][source_table] == "table 1" { … }` to direct the events to the correct output.

---

<div class="post-metadata">

**Author:** ![Krishna\_Sai\_Nag\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krishna_sai_nag_g/32/80467_2.png) [@Krishna\_Sai\_Nag\_G](https://discuss.elastic.co/u/Krishna_Sai_Nag_G)\
**Post date:** [July 23, 2020, 5:50am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/3 "2020-07-23T05:50:47Z")

</div>

@Jenni  
Hi Jenni

I have my logstash config file as below, i have to load table1 and table2 into different indexes, how can i do that.  
Can u please help me on this.

input {  
jdbc {  
jdbc\_driver\_library =\> "/Users/logstash/mysql-connector-java-5.1.39-bin.jar"  
jdbc\_driver\_class =\> "com.mysql.jdbc.Driver"  
jdbc\_connection\_string =\> "jdbc:mysql://localhost:3306/database\_name"  
jdbc\_user =\> "root"  
jdbc\_password =\> "password"  
statement =\> "select \* from table1"  
type =\> "table1"  
}  
jdbc {  
jdbc\_driver\_library =\> "/Users/logstash/mysql-connector-java-5.1.39-bin.jar"  
jdbc\_driver\_class =\> "com.mysql.jdbc.Driver"  
jdbc\_connection\_string =\> "jdbc:mysql://localhost:3306/database\_name"  
jdbc\_user =\> "root"  
jdbc\_password =\> "password"

```
statement => "select * from table2"
type => "table2"

```

}

}  
output {  
elasticsearch {  
index =\> "testdb"  
document\_type =\> "%{type}"  
hosts =\> "localhost:9200"  
}  
}

---

<div class="post-metadata">

**Author:** ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)\
**Post date:** [July 23, 2020, 6:10am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/4 "2020-07-23T06:10:42Z")

</div>

The syntax to add the meta information to your events in the input would be `add_field => {"[@metadata][index_name]" => "yourindexname"}`

---

<div class="post-metadata">

**Author:** ![Krishna\_Sai\_Nag\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krishna_sai_nag_g/32/80467_2.png) [@Krishna\_Sai\_Nag\_G](https://discuss.elastic.co/u/Krishna_Sai_Nag_G)\
**Post date:** [July 23, 2020, 6:13am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/5 "2020-07-23T06:13:12Z")

</div>

@Jenni  
If i add this in input field (add\_field =\> {"[@metadata][index\_name]" =\> "yourindexname"})

did i have to mention the index name in output filed also?

---

<div class="post-metadata">

**Author:** ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)\
**Post date:** [July 23, 2020, 6:14am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/6 "2020-07-23T06:14:24Z")

</div>

Yes, just like I said before 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 20, 2020, 6:14am UTC](https://discuss.elastic.co/t/how-to-load-tables-from-mysql-to-elasticsearch/242275/7 "2020-08-20T06:14:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
