camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > SQL Component
Date Thu, 10 Feb 2011 07:26:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2036/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/SQL+Component">SQL
Component</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h4. Using a JDBC based
idempotent repository <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color:
#dfd;">*Available as of Camel 2.7*:</span> In this section we will use the JDBC based
idempotent repository. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>First we need to setup
a {{javax.sql.DataSource}} in the spring XML file: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SQLComponent-SQLComponent"></a>SQL Component</h2>

<p>The <b>sql:</b> component allows you to work with databases using JDBC
queries. The difference between this component and <a href="/confluence/display/CAMEL/JDBC"
title="JDBC">JDBC</a> component is that in case of SQL the query is a property of
the endpoint and it uses message payload as parameters passed to the query.</p>

<p>This component uses <tt>spring-jdbc</tt> behind the scenes for the actual
SQL handling.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-sql&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;
</pre>
</div></div>

<h3><a name="SQLComponent-URIformat"></a>URI format</h3>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>The SQL component
can only be used to define producer endpoints. In other words, you cannot define an SQL endpoint
in a <tt>from()</tt> statement.</td></tr></table></div>

<p>The SQL component uses the following endpoint URI notation:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sql:select * from table where id=# order by name[?options]
</pre>
</div></div>

<p>Notice that the standard <tt>?</tt> symbol that denotes the parameters
to an SQL query is substituted with the <tt>#</tt> symbol, because the <tt>?</tt>
symbol is used to specify options for the endpoint. The <tt>?</tt> symbol replacement
can be configured on endpoint basis.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="SQLComponent-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>dataSourceRef</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.5.1/2.0:</b> Reference to a <tt>DataSource</tt>
to look up in the registry. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>placeholder</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>#</tt> </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Specifies a character that will
be replaced to <tt>?</tt> in SQL query. Notice, that it is simple <tt>String.replaceAll()</tt>
operation and no SQL parsing is involved (quoted strings will also change) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>template.&lt;xxx&gt;</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Sets additional options on the Spring <tt>JdbcTemplate</tt>
that is used behind the scenes to execute the queries. For instance, <tt>template.maxRows=10</tt>.
For detailed documentation, see the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html"
class="external-link" rel="nofollow">JdbcTemplate javadoc</a> documentation. </td>
</tr>
</tbody></table>
</div>


<h3><a name="SQLComponent-Treatmentofthemessagebody"></a>Treatment of the
message body</h3>

<p>The SQL component tries to convert the message body to an object of <tt>java.util.Iterator</tt>
type and then uses this iterator to fill the query parameters (where each query parameter
is represented by a <tt>#</tt> symbol (or configured placeholder) in the endpoint
URI). If the message body is not an array or collection, the conversion results in an iterator
that iterates over only one object, which is the body itself.</p>

<p>For example, if the message body is an instance of <tt>java.util.List</tt>,
the first item in the list is substituted into the first occurrence of <tt>#</tt>
in the SQL query, the second item in the list is substituted into the second occurrence of
<tt>#</tt>, and so on.</p>

<h3><a name="SQLComponent-Resultofthequery"></a>Result of the query</h3>

<p>For <tt>select</tt> operations, the result is an instance of <tt>List&lt;Map&lt;String,
Object&gt;&gt;</tt> type, as returned by the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html#queryForList(java.lang.String,%20java.lang.Object%91%93)"
class="external-link" rel="nofollow">JdbcTemplate.queryForList()</a> method. For
<tt>update</tt> operations, the result is the number of updated rows, returned
as an <tt>Integer</tt>.</p>

<h3><a name="SQLComponent-Headervalues"></a>Header values</h3>

<p>When performing <tt>update</tt> operations, the SQL Component stores
the update count in the following message headers:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>SqlProducer.UPDATE_COUNT</tt> </td>
<td class='confluenceTd'> Camel 1.x: The number of rows updated for <tt>update</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlUpdateCount</tt> </td>
<td class='confluenceTd'> Camel 2.0: The number of rows updated for <tt>update</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelSqlRowCount</tt> </td>
<td class='confluenceTd'> Camel 2.0: The number of rows returned for <tt>select</tt>
operations, returned as an <tt>Integer</tt> object. </td>
</tr>
</tbody></table>
</div>


<h3><a name="SQLComponent-ConfigurationinCamel1.5.0orlower"></a>Configuration
in Camel 1.5.0 or lower</h3>

<p>The SQL component must be configured before it can be used. In Spring, you can configure
it as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"sql"</span>
class=<span class="code-quote">"org.apache.camel.component.sql.SqlComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"dataSource"</span>
ref=<span class="code-quote">"myDS"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myDS"</span>
class=<span class="code-quote">"org.springframework.jdbc.datasource.DriverManagerDataSource"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"driverClassName"</span>
value=<span class="code-quote">"com.mysql.jdbc.Driver"</span> /&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"url"</span>
value=<span class="code-quote">"jdbc:mysql://localhost:3306/ds"</span> /&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"username"</span>
value=<span class="code-quote">"username"</span> /&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"password"</span>
value=<span class="code-quote">"password"</span> /&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="SQLComponent-ConfigurationinCamel1.5.1orhigher"></a>Configuration
in Camel 1.5.1 or higher</h3>
<p>You can now set a reference to a <tt>DataSource</tt> in the URI directly:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sql:select * from table where id=# order by name?dataSourceRef=myDS
</pre>
</div></div>


<h3><a name="SQLComponent-Sample"></a>Sample</h3>
<p>In the sample below we execute a query and retrieve the result as a <tt>List</tt>
of rows, where each row is a <tt>Map&lt;String, Object</tt> and the key is
the column name. </p>

<p>First, we set up a table to use for our sample. As this is based on an unit test,
we do it java code:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// <span class="code-keyword">this</span>
is the database we create with some initial data <span class="code-keyword">for</span>
our unit test
</span>jdbcTemplate.execute(<span class="code-quote">"create table projects (id
integer primary key,"</span>
                     + <span class="code-quote">"project varchar(10), license varchar(5))"</span>);
jdbcTemplate.execute(<span class="code-quote">"insert into projects values (1, 'Camel',
'ASF')"</span>);
jdbcTemplate.execute(<span class="code-quote">"insert into projects values (2, 'AMQ',
'ASF')"</span>);
jdbcTemplate.execute(<span class="code-quote">"insert into projects values (3, 'Linux',
'XXX')"</span>);
</pre>
</div></div>

<p>Then we configure our route and our <tt>sql</tt> component. Notice that
we use a <tt>direct</tt> endpoint in front of the <tt>sql</tt> endpoint.
This allows us to send an exchange to the <tt>direct</tt> endpoint with the URI,
<tt>direct:simple</tt>, which is much easier for the client to use than the long
<tt>sql:</tt> URI. Note that the <tt>DataSource</tt> is looked up
up in the registry, so we can use standard Spring XML to configure our <tt>DataSource</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:simple"</span>)
    .to(<span class="code-quote">"sql:select * from projects where license = # order
by id?dataSourceRef=jdbc/myDataSource"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And then we fire the message into the <tt>direct</tt> endpoint that will
route it to our <tt>sql</tt> component that queries the database.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">MockEndpoint mock = getMockEndpoint(<span class="code-quote">"mock:result"</span>);
mock.expectedMessageCount(1);

<span class="code-comment">// send the query to direct that will route it to the sql
where we will execute the query
</span><span class="code-comment">// and bind the parameters with the data from
the body. The body only contains one value
</span><span class="code-comment">// in <span class="code-keyword">this</span>
<span class="code-keyword">case</span> (XXX) but <span class="code-keyword">if</span>
we should use multi values then the body will be iterated
</span><span class="code-comment">// so we could supply a List&lt;<span
class="code-object">String</span>&gt; instead containing each binding value.
</span>template.sendBody(<span class="code-quote">"direct:simple"</span>,
<span class="code-quote">"XXX"</span>);

mock.assertIsSatisfied();

<span class="code-comment">// the result is a List
</span>List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody());

<span class="code-comment">// and each row in the list is a Map
</span>Map row = assertIsInstanceOf(Map.class, received.get(0));

<span class="code-comment">// and we should be able the get the project from the map
that should be Linux
</span>assertEquals(<span class="code-quote">"Linux"</span>, row.get(<span
class="code-quote">"PROJECT"</span>));
</pre>
</div></div>

<p>We could configure the <tt>DataSource</tt> in Spring XML as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
 <span class="code-tag">&lt;jee:jndi-lookup id=<span class="code-quote">"myDS"</span>
jndi-name=<span class="code-quote">"jdbc/myDataSource"</span>/&gt;</span>

</pre>
</div></div>

<h4><a name="SQLComponent-UsingaJDBCbasedidempotentrepository"></a>Using
a JDBC based idempotent repository</h4>
<p><b>Available as of Camel 2.7</b>: In this section we will use the JDBC
based idempotent repository. </p>

<p>First we need to setup a <tt>javax.sql.DataSource</tt> in the spring
XML file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"dataSource"</span>
class=<span class="code-quote">"org.springframework.jdbc.datasource.SingleConnectionDataSource"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"driverClassName"</span>
value=<span class="code-quote">"org.hsqldb.jdbcDriver"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"url"</span>
value=<span class="code-quote">"jdbc:hsqldb:mem:camel_jdbc"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"username"</span>
value=<span class="code-quote">"sa"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"password"</span>
value=""/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>And finally we can create our JDBC idempotent repository in the spring XML file as
well:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"messageIdRepository"</span>
class=<span class="code-quote">"org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository"</span>&gt;</span>
	<span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"dataSource"</span>
/&gt;</span>
	<span class="code-tag">&lt;constructor-arg value=<span class="code-quote">"myProcessorName"</span>
/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;camel:camelContext&gt;</span>
	<span class="code-tag">&lt;camel:errorHandler id=<span class="code-quote">"deadLetterChannel"</span>
type=<span class="code-quote">"DeadLetterChannel"</span> deadLetterUri=<span
class="code-quote">"mock:error"</span>&gt;</span>
		<span class="code-tag">&lt;camel:redeliveryPolicy maximumRedeliveries=<span
class="code-quote">"0"</span> maximumRedeliveryDelay=<span class="code-quote">"0"</span>
logStackTrace=<span class="code-quote">"false"</span> /&gt;</span>
	<span class="code-tag">&lt;/camel:errorHandler&gt;</span>
	
	<span class="code-tag">&lt;camel:route id=<span class="code-quote">"JdbcMessageIdRepositoryTest"</span>
errorHandlerRef=<span class="code-quote">"deadLetterChannel"</span>&gt;</span>
		<span class="code-tag">&lt;camel:from uri=<span class="code-quote">"direct:start"</span>
/&gt;</span>
		<span class="code-tag">&lt;camel:idempotentConsumer messageIdRepositoryRef=<span
class="code-quote">"messageIdRepository"</span>&gt;</span>
			<span class="code-tag">&lt;camel:header&gt;</span>messageId<span
class="code-tag">&lt;/camel:header&gt;</span>
			<span class="code-tag">&lt;camel:to uri=<span class="code-quote">"mock:result"</span>
/&gt;</span>
		<span class="code-tag">&lt;/camel:idempotentConsumer&gt;</span>
	<span class="code-tag">&lt;/camel:route&gt;</span>
<span class="code-tag">&lt;/camel:camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="SQLComponent-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/JDBC" title="JDBC">JDBC</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SQL+Component">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=82932&revisedVersion=16&originalVersion=15">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SQL+Component?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message