Language Integrated Queries In Ballerina
Language integrated queries specify the logic in SQL-like syntax to process the data/events, and they are easy to write and understand due to the simple syntax.
Join the DZone community and get the full member experience.Join For Free
Data processing is one of the critical components of enterprise integration. Even though there are various approaches to process data, query expressions are usually considered the go-to approach. Language integrated queries specify the logic in SQL-like syntax to process the data/events, and they are easy to write and understand due to the simplicity of the syntax. The Ballerina programming language provides first-class support to write queries for data processing. In this article, we will discuss the query language support in Ballerina with some examples.
As of now, language integrated queries are supported for iterator implementations such as an array, map, stream, and table. There are two kinds of integrated queries that can be written in Ballerina — query expression, which allows generating a list, table, string or XML; stream and query action, which executes a set of statements for each element of the iterator.
Query expressions contain a set of clauses similar to SQL to process the data. Query expressions must start with the `from` clause and can perform various operations such as filter, join, sort, limit, and projection. There are various SQL like clauses to perform these operations.
- `from` clause - Defines an input source/iterator
- `where` clause - Defines a filtering condition
- `let` clause - Defines a variable that could be used in the subsequent clauses
- `join` clause - Defines an input source/iterator for joining purposes. It performs an inner or left outer equijoin.
- `order by` clause - Defines the sorting logic
- `limit` clause - Limits the number of elements passed to the next clause
- `select` clause - Performs the projection. Outputs a list, table, string, XML, or stream.
- `on conflict` clause - Allows only for a query expression that constructs a table with a key sequence to avoid duplicates
Example 1 - Query expression that constructs a list
Example 2 - Query expression that constructs a table
Please refer to the Queries section in Ballerina By Examples for more information: https://ballerina.io/swan-lake/learn/by-example/..
Query actions are executed in the same way as the clauses in the query expression, but it doesn’t generate an output such as a list. It rather executes a set of statements defined by the user.
Example 3 - Query action
Writing Integrated Queries
The example below, related to an online product order use case, gives you a better understanding of Ballerina integrated queries. Refer the below code snippets for better understanding.
- There is an HTTP service to which users can request the product details. The payload is a JSON array that represents the product details.
- Then the product details array (iterator) is manipulated using query expressions to find the price of each product in the iterator. Here, as per the query, we only consider the grocery products. In this process there is a `join` with the table which contains the product prices.
- Next, an integrated query is used to calculate the total bill amount.
- Finally, the total bill amount is sent as the response to the HTTP request made.
The complete Ballerina code can be found below.
Try It Out
Follow the steps below to try out the Ballerina example.
Note: These steps are given assuming the Ballerina runtime is already installed locally.
1. Copy the Ballerina code above and create a .bal file. (e.g., order_service.bal)
2. Then, execute the command below to start the HTTP service, which accepts product order requests.
3. Invoke the HTTP service by sending the product order details in the payload.
4. Then, you will receive a response that gives the total grocery bill amount.
In this article, we have discussed Ballerina's integrated query capabilities and provided a few examples for better understanding. As of now, integrated query support is available only for iterator objects and we are working on providing query support for unbounded streams (i.e., event stream processing).
For more information on Ballerina's integrated queries, please refer to the Ballerina by Examples in https://ballerina.io/swan-lake/learn/by-example/.
Published at DZone with permission of Mohanadarshan Vivekanandalingam. See the original article here.
Opinions expressed by DZone contributors are their own.