DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Improved Debuggability for Couchbase's SQL++ User-Defined Functions
  • Securing AWS RDS SQL Server for Retail: Comprehensive Strategies and Implementation Guide
  • Essential Techniques for Performance Tuning in Snowflake
  • Simplifying SQL Server and Kubernetes Across Hybrid/Multi-Cloud

Trending

  • How to Submit a Post to DZone
  • Service Mesh Unleashed: A Riveting Dive Into the Istio Framework
  • API Appliance for Extreme Agility and Simplicity
  • Some Thoughts on Bad Programming Practices
  1. DZone
  2. Data Engineering
  3. Databases
  4. Camel SQL Stored Procedure Example in Red Hat Fuse on Karaf

Camel SQL Stored Procedure Example in Red Hat Fuse on Karaf

Learn what you need to know about Came SQL stored procedures.

By 
Chandra Shekhar Pandey user avatar
Chandra Shekhar Pandey
·
Jun. 15, 21 · Tutorial
Like (2)
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will demonstrate an example of Camel SQL Stored Procedure. 

I tested this POC in Fedora 34 and used the following product/technologies.

  1. Red Hat Fuse on Karaf 7.8
  2. MariaDB on Podman
  3. The Camel code is in my GitHub repository.

So let the adventure begin.

Step 1: Setup MariaDB on Podman. 

Here we will expose the 3306 port so that the external client and Camel code connect to MariaDB. We have set the environment variable MYSQL_ROOT_PASSWORD=mypassword, which we will use as a password field so that the external client or camel code can connect to the database.

Shell
 
$ podman run -p 3306:3306 -d --name=mariadb -e MYSQL_ROOT_PASSWORD=mypassword mariadb/server
f63034a1f205d00882cba109f4a131bcc8d24858113a2b58dc3c746535339211

$ podman ps -a
CONTAINER ID  IMAGE                            COMMAND  CREATED        STATUS            PORTS                   NAMES
f63034a1f205  docker.io/mariadb/server:latest  mysqld   5 seconds ago  Up 3 seconds ago  0.0.0.0:3306->3306/tcp  mariadb


Step 2: Access MariaDB, create the database and table, and insert some data.

Shell
 
$ podman exec -it mariadb bash
root@f63034a1f205:/# mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.5.10-MariaDB-1:10.5.10+maria~bionic mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database testdb
    -> ;
Query OK, 1 row affected (0.000 sec)


MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]> create table item(id INT NOT NULL AUTO_INCREMENT, title VARCHAR(50) NOT NULL, description VARCHAR(200), price INT NOT NULL, create_date DATE, PRIMARY KEY(id)); 
Query OK, 0 rows affected (0.197 sec)

MariaDB [testdb]> insert into item values(1, "pencilbox", "plastic one side box", 40, '2021-06-12');
Query OK, 1 row affected (0.126 sec)
MariaDB [testdb]> 
MariaDB [testdb]> insert into item values(2, "eraser", "eraset", 5, '2021-06-11');
Query OK, 1 row affected (0.126 sec)

MariaDB [testdb]> insert into item values(3, "sharpener", "sharpener", 6, '2021-06-11');
Query OK, 1 row affected (0.352 sec)

MariaDB [testdb]> insert into item values(4, "pencil", "pencil", 3, '2021-06-13');
Query OK, 1 row affected (0.010 sec)

MariaDB [testdb]> select * from item;
+----+-----------+----------------------+-------+-------------+
| id | title     | description          | price | create_date |
+----+-----------+----------------------+-------+-------------+
|  1 | pencilbox | plastic one side box |    40 | 2021-06-12  |
|  2 | eraser    | eraset               |     5 | 2021-06-11  |
|  3 | sharpener | sharpener            |     5 | 2021-06-11  |
|  4 | pencil    | pencil               |     3 | 2021-06-13  |
+----+-----------+----------------------+-------+-------------+
4 rows in set (0.000 sec)


MariaDB [testdb]> select * from item where price >= 5;
+----+-----------+----------------------+-------+-------------+
| id | title     | description          | price | create_date |
+----+-----------+----------------------+-------+-------------+
|  1 | pencilbox | plastic one side box |    40 | 2021-06-12  |
|  2 | eraser    | eraset               |     5 | 2021-06-11  |
|  3 | sharpener | sharpener            |     5 | 2021-06-11  |
+----+-----------+----------------------+-------+-------------+
3 rows in set (0.001 sec)


Step 3: Create a stored procedure in MariaDB.

Shell
 
# Create Stored-Procedure
MariaDB [testdb]> DELIMITER &&
MariaDB [testdb]> CREATE PROCEDURE testdb.GetItems(IN cost INT)
    -> BEGIN
    -> SELECT * from item where price > cost;
    -> END &&
Query OK, 0 rows affected (0.062 sec)

# This is how we can call stored-procedure from MariaDB console.
MariaDB [testdb]> call GetItems(4);
+----+-----------+----------------------+-------+-------------+
| id | title     | description          | price | create_date |
+----+-----------+----------------------+-------+-------------+
|  1 | pencilbox | plastic one side box |    40 | 2021-06-12  |
|  2 | eraser    | eraset               |     5 | 2021-06-11  |
|  3 | sharpener | sharpener            |     6 | 2021-06-11  |
+----+-----------+----------------------+-------+-------------+
3 rows in set (0.001 sec)

Query OK, 0 rows affected (0.001 sec)

MariaDB [testdb]> call GetItems(5);
+----+-----------+----------------------+-------+-------------+
| id | title     | description          | price | create_date |
+----+-----------+----------------------+-------+-------------+
|  1 | pencilbox | plastic one side box |    40 | 2021-06-12  |
|  3 | sharpener | sharpener            |     6 | 2021-06-11  |
+----+-----------+----------------------+-------+-------------+
2 rows in set (0.001 sec)

# This is how we can view stored-procedure.
MariaDB [(none)]> select * from  information_schema.routines where SPECIFIC_NAME="GetItems";
+---------------+-----------------+----------------+--------------+--------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+----------------+--------------+---------------------------------------------------+---------------+-------------------+-----------------+------------------+-----------------+----------+---------------+---------------------+---------------------+--------------------------------------------------------------------------------------------------------+-----------------+---------+----------------------+----------------------+--------------------+
| SPECIFIC_NAME | ROUTINE_CATALOG | ROUTINE_SCHEMA | ROUTINE_NAME | ROUTINE_TYPE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | DATETIME_PRECISION | CHARACTER_SET_NAME | COLLATION_NAME | DTD_IDENTIFIER | ROUTINE_BODY | ROUTINE_DEFINITION                                | EXTERNAL_NAME | EXTERNAL_LANGUAGE | PARAMETER_STYLE | IS_DETERMINISTIC | SQL_DATA_ACCESS | SQL_PATH | SECURITY_TYPE | CREATED             | LAST_ALTERED        | SQL_MODE                                                                                               | ROUTINE_COMMENT | DEFINER | CHARACTER_SET_CLIENT | COLLATION_CONNECTION | DATABASE_COLLATION |
+---------------+-----------------+----------------+--------------+--------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+----------------+--------------+---------------------------------------------------+---------------+-------------------+-----------------+------------------+-----------------+----------+---------------+---------------------+---------------------+--------------------------------------------------------------------------------------------------------+-----------------+---------+----------------------+----------------------+--------------------+
| GetItems      | def             | testdb         | GetItems     | PROCEDURE    |           |                     NULL |                   NULL |              NULL |          NULL |               NULL | NULL               | NULL           | NULL           | SQL          | BEGIN
	SELECT * from item where price > cost;
END | NULL          | NULL              | SQL             | NO               | CONTAINS SQL    | NULL     | DEFINER       | 2021-06-12 07:23:43 | 2021-06-12 07:23:43 | IGNORE_SPACE,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |                 | root@%  | utf8mb4              | utf8mb4_general_ci   | utf8mb4_general_ci |
+---------------+-----------------+----------------+--------------+--------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+----------------+--------------+---------------------------------------------------+---------------+-------------------+-----------------+------------------+-----------------+----------+---------------+---------------------+---------------------+--------------------------------------------------------------------------------------------------------+-----------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.004 sec)


Step 4: Write a Camel route to access MariaDB and call the stored procedure. 

You can find actual code in this GitHub repository.

XML
 
<?xml version="1.0" encoding="UTF-8"?>

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="              http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd              http://camel.apache.org/schema/blueprint https://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <camelContext id="cbr-example-context" xmlns="http://camel.apache.org/schema/blueprint">

        <route id="cbr-route">
            <from id="_from1" uri="timer://foo?fixedRate=true&period=20000"/>
            <setHeader headerName="num1"><constant>6</constant></setHeader>
            <to uri="sql-stored:GetItems(INTEGER ${headers.num1})?dataSource=#dbcp"/>
            <log id="_log5" message="headers: ${headers} Body: ${body}"/>
        </route>
    </camelContext>
    <bean id="dbcp" destroy-method="close"
		class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://0.0.0.0:3306/testdb" />
		<property name="username" value="root" />
		<property name="password" value="mypassword" />
	</bean>
</blueprint>


Step 4: Start and Access Red Hat Fuse on Karaf 7.8.

Shell
 
# We have to first build this project with following command, we should run this from the location of pom.xml in project.
[chandrashekhar@localhost Camel-SqlStoredProcedure-Example]$ mvn clean install

# Within [FUSE_HOME]/bin start Red Hat Fuse 7.8
[chandrashekhar@localhost bin]$ ./start
[chandrashekhar@localhost bin]$ ./status
karaf: JAVA_HOME not set; results may vary
Running ...
[chandrashekhar@localhost bin]$ cd ../etc
[chandrashekhar@localhost etc]$ vi users.properties
# uncomment following two lines
admin = admin,_g_:admingroup
_g_\:admingroup = group,admin,manager,viewer,systembundles,ssh
[chandrashekhar@localhost etc]$ cd ../bin
[chandrashekhar@localhost bin]$ ./client
client: JAVA_HOME not set; results may vary
Logging in as admin

 ____          _   _   _       _     _____                
|  _ \ ___  __| | | | | | __ _| |_  |  ___|   _ ___  ___  
| |_) / _ \/ _` | | |_| |/ _` | __| | |_ | | | / __|/ _ \ 
|  _ <  __/ (_| | |  _  | (_| | |_  |  _|| |_| \__ \  __/ 
|_| \_\___|\__,_| |_| |_|\__,_|\__| |_|   \__,_|___/___| 

  Red Hat Fuse (7.8.0.fuse-780038-redhat-00001)
  http://www.redhat.com/products/jbossenterprisemiddleware/fuse/

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.

Open a browser to http://localhost:8181/hawtio to access the management console

Hit '<ctrl-d>' or 'shutdown' to shutdown Red Hat Fuse.

admin@root()> 


Step 5: Install dependencies and applications in Red Hat Fuse on Karaf 7.8.

Shell
 
admin@root()> install -s mvn:org.apache.commons/commons-dbcp2/2.1.1
Bundle ID: 248
admin@root()> install -s mvn:org.apache.commons/commons-pool2/2.6.2
Bundle ID: 249
admin@root()> feature:install camel-sql
admin@root()> install -s mvn:org.mariadb.jdbc/mariadb-java-client/2.7.3
Bundle ID: 251
admin@root()> install -s mvn:com.mycompany/sql-storedprocedure/1.0
Bundle ID: 252
admin@root()> 


Step 6: Check Red Hat Fuse logs.

Java
 
$ cd fuse-karaf-7.8.0.fuse-780038-redhat-00001/data/log

# In logs we will see records returned by stored procedure.
$ tail -f fuse.log
2021-06-13 10:35:43,955 | INFO  |  Event Dispatcher: 1 | o.a.c.b.BlueprintCamelContext    | 64 - org.apache.camel.camel-core - 2.23.2.fuse-780036-redhat-00001 | Route: cbr-route started and consuming from: timer://foo?fixedRate=true&period=20000
2021-06-13 10:35:43,957 | INFO  |  Event Dispatcher: 1 | o.a.c.b.BlueprintCamelContext    | 64 - org.apache.camel.camel-core - 2.23.2.fuse-780036-redhat-00001 | Total 1 routes, of which 1 are started
2021-06-13 10:35:43,958 | INFO  |  Event Dispatcher: 1 | o.a.c.b.BlueprintCamelContext    | 64 - org.apache.camel.camel-core - 2.23.2.fuse-780036-redhat-00001 | Apache Camel 2.23.2.fuse-780036-redhat-00001 (CamelContext: cbr-example-context) started in 0.320 seconds
2021-06-13 10:35:45,177 | INFO  | ead #1 - timer://foo | cbr-route                        | 64 - org.apache.camel.camel-core - 2.23.2.fuse-780036-redhat-00001 | headers: {breadcrumbId=ID-localhost-localdomain-1623560743418-0-1, CamelSqlStoredUpdateCount=0, firedTime=Sun Jun 13 10:35:44 IST 2021, num1=6} Body: {#result-set-1=[{id=1, title=pencilbox, description=plastic one side box, price=40, create_date=2021-06-12}], #update-count-1=0}
2021-06-13 10:36:04,959 | INFO  | ead #1 - timer://foo | cbr-route                        | 64 - org.apache.camel.camel-core - 2.23.2.fuse-780036-redhat-00001 | headers: {breadcrumbId=ID-localhost-localdomain-1623560743418-0-2, CamelSqlStoredUpdateCount=0, firedTime=Sun Jun 13 10:36:04 IST 2021, num1=6} Body: {#result-set-1=[{id=1, title=pencilbox, description=plastic one side box, price=40, create_date=2021-06-12}], #update-count-1=0}


That's it, guys. In this POC, we learned how we can quickly set up MariaDB using Podman. We also learned how we can write a Camel application and deploy it in Red Hat Fuse on Karaf.

sql

Opinions expressed by DZone contributors are their own.

Related

  • Improved Debuggability for Couchbase's SQL++ User-Defined Functions
  • Securing AWS RDS SQL Server for Retail: Comprehensive Strategies and Implementation Guide
  • Essential Techniques for Performance Tuning in Snowflake
  • Simplifying SQL Server and Kubernetes Across Hybrid/Multi-Cloud

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: