Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join tables in r2dbc?

In java reactor, r2dbc. I have two tables A, B. I also have repositories for them defined. How can i get data made up of A join B?

I only come up with the following approach: call databaseClient.select from A and consequently in a loop call select from B.

But i want more efficient and reactive way. How to do it?

like image 342
voipp Avatar asked Feb 10 '20 07:02

voipp


People also ask

What is a joined-table in DB2?

A joined-table specifies an intermediate result table that is the result of either an inner join or an outer join. The table is derived by applying one of the join operators—INNER, FULL OUTER, LEFT OUTER, or RIGHT OUTER—to its operands. Db2 supports inner joins and outer joins (left, right, and full).

Why are there only three rows in the joined table?

You probably noticed that in the join we just performed, there were only three rows in the joined table. That’s because we performed something called an “inner join”, where R only returns the data frame rows that match up with the other data frame. If you were to visualize this type of join, it would look something like this:

How do I find a single customer in r2dbc?

Next, it calls findAll () to fetch all Customer objects from the database. Then it calls findById () to fetch a single Customer by its ID. Finally, it calls findByLastName () to find all customers whose last name is "Bauer". R2DBC is a reactive programming technology.

How do I join two tables together?

Importantly, to join two different tables together, you need to make sure you have a column in common between both data sets. This common column is called a “key”, and it should provide a unique identifier for every row. In the case of our data, the “student” column is our key, and it provides a unique number for each student.


1 Answers

TL;DR: Using SQL.

Spring Data's DatabaseClient is an improved and reactive variant for R2DBC of what JdbcTemplate is for JDBC. It encapsulates various execution modes, resource management, and exception translation. Its fluent API select/insert/update/delete methods are suitable for simple and flat queries. Everything that goes beyond the provided API is subject to SQL usage.

That being said, the method you're looking for is DatabaseClient.execute(…):

DatabaseClient client = …;
client.execute("SELECT person.age, address.street FROM person INNER JOIN address ON person.address = address.id");

The exact same goes for repository @Query methods.

Calling the database during result processing is a good way to lock up the entire result processing as results are fetched stream-wise. Issuing a query while not all results are fetched yet can exhaust the prefetch buffer of 128 or 256 items and cause your result stream to stuck. Additionally, you're creating an N+1 problem.

like image 53
mp911de Avatar answered Dec 16 '22 21:12

mp911de