Parsing the Java 8 stream into SQL

Source: Internet
Author: User
Tags stream api java 8 stream

How do we solve performance problems when we try to use the database in the Java 8 way?

By partnering with Zeroturnaround, we've brought you a Java Zone. You can read the 8-step guide here to see how to improve your productivity when writing code, using the process of skipping slow application redeployment and implementing Application analytics.

When Java 8 was released, people began to make everything flow, and before long they began to imagine how great it would be if they could use the same way in the database. Essentially, a database is made up of large chunks of data organized in a form-like structure. These structures are ideal for filtering and mapping operations, as shown in SQL Select,where, and as statements. The first thing people do is to get a large collection of data from the database and then use JAVA8 's new cool stream technology to process the data.


There was a problem immediately, and the problem was to transfer all rows of data from the database to the database, which was too long. The consequence is that there is not much surplus left to handle the data in memory. Even if you can do something really surprising with the new tools in Java 8, this benefit is not even useful for database applications because of the performance cost.

When I started thinking about speedment open source projects, I soon realized the potential of using the database in Java 8, but we really needed a smart way to deal with performance issues. In this article I'll show you how to use a custom stream API proxy to manipulate a stream in the background and optimize the results of SQL queries to solve this problem.

!

Suppose you have a user table on a remote database server and you want to print a user name that is older than 70 years old. Using Speedment in Java 8 might be the case:

?
12345 finalUserManager users = speedment.managerOf(User.class);users.stream()    .filter(User.AGE.greaterThan(70))    .map(User.NAME.get())    .forEach(System.out::println);

Look at your code. Not only do you have to chill first, my program will download the entire data sheet from the database, and then do the filtering on the client? What happens if I have 100,000,000 users? Network latency is enough to kill the app! Wow, that's not true, just like I said before. Speedment analyzes the stream before it ends.

Let's see what's going on behind the scenes. The method in Usermanager. Stream () returns a custom implementation of a stream interface that contains all of the stream's no data until the stream is closed, and the metadata can be used to optimize the stream for events that are terminated. When the method. ForEach is called, the pipeline looks like this:


Terminate activity (in this example, ForEach will traverse the pipeline backwards to see if it can be optimized, first it encounters a mapping from User to String.) Speedment recognizes that it is a Getter function, and the User.Name field is used to generate it. Getter can parse to SQL, so the terminating activity switches to read operations because the NAME column and the map are removed.

Next is. Filter. Filter is also identified as a custom action, in this case a predicate. Because it is a custom implementation, it can contain all the metadata required in the SQL query, so it can be safely removed from the stream and appended to the write operation.


At this point, the termination activity continues to find the pipeline, it will find the source of the stream, once it reaches the source of the stream, the read operation resolves to SQL and commits to the SQL management system. The last stream<string> will be terminated by the most. ForEach. The above content will generate the exact SQL:

?
1 SELECT `name` FROM `User` WHERE `User`.`age` > 70;

Java code does not need to change and special operation!

This is a simple example of how a stream can be simplified using a custom implementation before speedment is executed. You are welcome to check the source code to find a better way to optimize this technique. It really helps us improve system performance and work in any distributed environment that can be Java-8.

============= for reference only ========

Parsing the Java 8 stream into SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.