# Join 3 tables with SQL in order to index with ES using Logstash

**URL:** <https://discuss.elastic.co/t/join-3-tables-with-sql-in-order-to-index-with-es-using-logstash/99129>\
**Category:** Logstash\
**Created:** [September 1, 2017, 12:55pm UTC](https://discuss.elastic.co/t/join-3-tables-with-sql-in-order-to-index-with-es-using-logstash/99129 "2017-09-01T12:55:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![malhotras](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malhotras](https://discuss.elastic.co/u/malhotras)\
**Post date:** [September 1, 2017, 12:55pm UTC](https://discuss.elastic.co/t/join-3-tables-with-sql-in-order-to-index-with-es-using-logstash/99129/1 "2017-09-01T12:55:19Z")

</div>

I have 3 tables which I would like to index with ES using Logstash. My table structure looks like this:

```
Table A:
  ID | Name
----- | ------
28254 | Abc
28234 | Cdf
5228 | ztr
4195 | Gre
5220 | tds
5224 | cbc
 Table B:
 ID | Name | A_id | B_id |
 ----- | -------|--------|-----------
 1 | qrl | 28254 | 28241 |
 2 | sdf | 5228 | 20983 |
 3 | cde | 28254 | 27904 |
4 | vdf | 28234 | 24522 |
5 | vfr | 28234 | 28241 |
6 | gdf | 4195 | 6501 |
7 | bdr | 4195 | 5669 |
8 | yrf | 5220 | 6501 |
9 | cbc | 5220 | 28241 |
10 | hre | 5224 | 27904 |
Table C:
A_ID | C_ID
----- | ------
28254 | 1220
28234 | 1083
4195 | 404
5220 | 473
5224 | 473
5228 | 1220

```

So, a\_id can have many b\_id. And one b\_id can be associated with many a\_id. Similarly, a\_id can be assosiated with many c\_id and one c\_id can be assosiated with many a\_id. There is no relation between b\_id and c\_id.

How would I be able to define an appropriate relation with SQL statement for these 3 tables. And, use that statement in Logstash to create a nested structure with A as a parent, B as a child and C as fields in A.

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [September 4, 2017, 8:30am UTC](https://discuss.elastic.co/t/join-3-tables-with-sql-in-order-to-index-with-es-using-logstash/99129/2 "2017-09-04T08:30:39Z")

</div>

You will have to do 1 JDBC input query and 1 lookup filter.

The JDBC input query is probably a left join between A and C with null for C\_ID if there is no match.

Do a look up on B using the jdbc-streaming filter. As you have multiple records with the same A\_ID in table B these will be added as a hash to the `target` field.

Your event should then look something like this:

```auto
{"id" => 5220, "name" => "tds", "c_id" => 473, "from_b" => [{"name" => "yrf"}, {"name" => "cbc"}]}

```

You will have add this to Logstash using `bin/logstash-plugin install logstash-filter-jdbc_streaming`

---

<div class="post-metadata">

**Author:** ![malhotras](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malhotras](https://discuss.elastic.co/u/malhotras)\
**Post date:** [September 4, 2017, 11:34am UTC](https://discuss.elastic.co/t/join-3-tables-with-sql-in-order-to-index-with-es-using-logstash/99129/3 "2017-09-04T11:34:57Z")

</div>

@guyboertje. Thanks for the useful hint. I was really stuck with mapping these 3 tables. It works like a charm!

---

<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:** [October 2, 2017, 11:35am UTC](https://discuss.elastic.co/t/join-3-tables-with-sql-in-order-to-index-with-es-using-logstash/99129/4 "2017-10-02T11:35:03Z")

</div>

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