Linking a Data Source to an Integration Flow
After creating a new data source, you must link it to the integration flow where it will be used.
To do this, follow the steps below:
-
Access or create the flow to which the data source will be linked.
If you have not yet created a flow, see: Creating a Source Integration. -
Click Add Step and then select the Components tab.
Use one of the components below to link a data source to your integration flow, depending on the type of operation you want to perform. Read the details and use cases for each component in the table below:
Component Use Case Query SQL (Recommended)
Dynamic queries (
SELECTs) using headers or body parameters. Ideal for externalizing queries.In the component itself (e.g.,
uri: sql:classpath:query.sql)JDBC
Direct operations (
INSERT,UPDATE) where the query is built in a previous step of the flow.The query must be in the message body at the time of the call.
SQL Stored Procedure
Execute complex business logic that already exists in the database (e.g.,
GET_USERS_BY_LASTNAME).In the component itself (e.g.,
uri: sql-stored:PROCEDURE_NAME(…)) -
Select the component and click CONFIRM or drag it to the diagram. The configuration form will open automatically.
-
With the configuration form open, manually add the exact name of the data source in the components:
-
For JDBC, find and enter the data source name in the Data Source Name field.
-
For SQL and SQL Stored Procedure, find and enter the data source name in the Data Source field.
For the SQL and SQL Stored Procedure components, you must include the "#" symbol before the data source name (e.g., #datasource-mysql-2).If a component (JDBC, SQL, or SQL Procedure) linked to a data source is deleted, the link between the data source and the flow will be automatically removed. This prevents the data source from being associated with a non-existent component. Likewise, if a flow linked to a data source is deleted, all links associated with that flow will be automatically removed.
-
-
Fill in the other fields in the form (such as the query or procedure name). See the parameters for each component.
-
Complete the configuration by clicking CONFIRM.
Components
| Click on the component names to access the official Apache Camel documentation. |
SQL (Recommended)
SQL is ideal for dynamic queries, where parameters are passed via headers or body, and the query can be externalized.
Parameters
| Parameter (UI) | Parameter (YAML) | Required | Description | Example |
|---|---|---|---|---|
Data Source Name |
parameters.dataSource |
Yes |
The exact name of the Data Source you created. |
datasource-mysql-2 |
SQL Query |
uri (path) |
Yes |
The query to be executed. It is recommended to use |
sql:classpath:query.sql |
Query Parameters |
(in the query) |
No |
Placeholders in your query (e.g., |
WHERE lastname = :#userLastName |
Example
-
The flow receives an HTTP POST request at the
/testendpoint. The request body must contain thelastNamefield. -
Then, it uses the value from
lastNameto create a header calleduserLastName, extracting the value with jsonpath ($.lastName). -
Next, it executes an SQL query defined in the
query.sqlfile (referenced viaclasspath), using the data source nameddatasource-mysql-2. -
Finally, it marshals the SQL query result to JSON, preparing the response to be sent to the client.
In summary: this flow receives a POST with a last name (
lastName), executes an SQL query using this value, and returns the result in JSON.
- from:
uri: rest:post:/test # the request body must contain the 'lastName' field.
steps:
- setHeader:
name: 'userLastName'
expression:
jsonpath:
expression: $.lastName
- to:
uri: 'sql:classpath:query.sql'
parameters:
dataSource: "#datasource-mysql-2"
- marshal:
json: {}
See below the contents of the query.sql file used in the flow above:
Below are the request and response bodies:
Request Body:
{
"lastName": "Doe"
}
Response Body:
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Jane"
}
]
JDBC
JDBC is used for more direct operations. The main difference is that it expects the SQL query to be in the message body at the time the component is called.
Parameters
| Parameter (UI) | Parameter (YAML) | Required | Description | Example |
|---|---|---|---|---|
Data Source |
uri (prefix) |
Yes |
The Data Source name prefixed by |
jdbc:datasource-mysql-2 |
SQL Query |
(implicit) |
Yes |
There is no query field. You must use a previous step (such as |
setBody → constant: SELECT … |
Example
-
The flow receives an HTTP POST request at the
/testendpoint. -
Then, it sets the message body as a constant string:
SELECT id, name FROM newTestes.users WHERE lastname = 'Wilson'. That is, it creates a fixed SQL query, searching for all users with the last name "Wilson". -
Next, it executes the above query in the database using the data source
datasource-mysql-2, through the JDBC component. -
Finally, it marshals the query result to JSON, preparing the response to be sent to the client.
In summary: this flow always executes the same SQL query (searching for users with the last name "Wilson") and returns the result in JSON.
- from:
uri: rest:post:/test
steps:
- setBody:
expression:
constant:
expression: SELECT id, name FROM newTestes.users WHERE lastname = 'Wilson'
- to:
uri: jdbc:datasource-mysql-2
- marshal:
json: {}
SQL Stored Procedure
SQL Stored Procedure is optimized for calling stored procedures in the database.
Parameters
| Parameter (UI) | Parameter (YAML) | Required | Description | Example |
|---|---|---|---|---|
Data Source |
parameters.dataSource |
Yes |
The exact name of the Data Source you created. |
datasource-mysql-5 |
Procedure Name |
uri (path) |
Yes |
The name of the procedure to be called, followed by its parameters. |
sql-stored:GET_USERS_BY_LASTNAME(…) |
Parameter Mapping |
(in uri) |
No |
Defines how Camel headers are mapped to the procedure parameters. |
(VARCHAR ${header.searchlastName}) |
Example
-
The flow receives an HTTP POST request at the
/test-stored-procedureendpoint. -
Then, it sets a header called
searchLastNamewith the constant value "Doe". -
Next, it calls the stored procedure
GET_USERS_BY_LASTNAME, passing the value of thesearchLastNameheader as a parameter (VARCHAR ${header.searchLastName}). The component used is SQL Store Procedure, which executes procedures in the database. -
Then, it uses the data source
datasource-mysql-5to connect to the database. -
Finally, it marshals the procedure result to JSON.
In summary: the flow executes a stored procedure in MySQL, passing a parameter, and returns the result in JSON.
from:
uri: rest:post:/test-stored-procedure
steps:
- setHeader:
name: searchLastName
expression:
constant: Doe
- to:
uri: sql-stored:GET_USERS_BY_LASTNAME(VARCHAR ${header.searchLastName})
parameters:
dataSource: "#datasource-mysql-5"
- marshal:
json: {}
Below is the procedure definition and the response body:
Procedure created in MySQL database:
DELIMITER $$
CREATE PROCEDURE GET_USERS_BY_LASTNAME(
IN p_lastname VARCHAR(100)
)
BEGIN
SELECT
id,
name,
lastname
FROM
users
WHERE
lastname = p_lastname;
END$$
DELIMITER ;
This procedure called GET_USERS_BY_LASTNAME, created in MySQL, receives an input parameter called p_lastname (of type VARCHAR).
When executed, it queries the users table, searching for all records whose lastname field matches the value received in p_lastname. The query result returns the id, name, and lastname fields of those users.
In summary, the procedure returns all users who have the last name provided as a parameter.
Response Body
{
"#result-set-1": [
{
"id": 1,
"name": "John",
"lastname": "Doe"
},
{
"id": 2,
"name": "Jane",
"lastname": "Doe"
}
],
"#update-count-1:0"
}
Share your suggestions with us!
Click here and then [+ Submit idea]