SQL ComponentPage edited by willem jiangSQL ComponentThe sql: component allows you to work with databases using JDBC queries. The difference between this component and JDBC 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. This component uses spring-jdbc behind the scenes for the actual SQL handling. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-sql</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
Treatment of the message bodyThe SQL component tries to convert the message body to an object of java.util.Iterator type and then uses this iterator to fill the query parameters (where each query parameter is represented by a # symbol 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. For example, if the message body is an instance of java.util.List, the first item in the list is substituted into the first occurrence of # in the SQL query, the second item in the list is substituted into the second occurrence of #, and so on. Result of the queryFor select operations, the result is an instance of List<Map<String, Object>> type, as returned by the JdbcTemplate.queryForList() method. For update operations, the result is the number of updated rows, returned as an Integer. Header valuesWhen performing update operations, the SQL Component stores the update count in the following message headers:
Configuration in Camel 1.5.0 or lowerThe SQL component must be configured before it can be used. In Spring, you can configure it as follows: <bean id="sql" class="org.apache.camel.component.sql.SqlComponent"> <property name="dataSource" ref="myDS"/> </bean> <bean id="myDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/ds" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean> Configuration in Camel 1.5.1 or higherYou can now set a reference to a DataSource in the URI directly: sql:select * from table where id=# order by name?dataSourceRef=myDS SampleIn the sample below we execute a query and retrieve the result as a List of rows, where each row is a Map<String, Object and the key is the column name. First, we set up a table to use for our sample. As this is based on an unit test, we do it java code: // this is the database we create with some initial data for our unit test jdbcTemplate.execute("create table projects (id integer primary key," + "project varchar(10), license varchar(5))"); jdbcTemplate.execute("insert into projects values (1, 'Camel', 'ASF')"); jdbcTemplate.execute("insert into projects values (2, 'AMQ', 'ASF')"); jdbcTemplate.execute("insert into projects values (3, 'Linux', 'XXX')"); Then we configure our route and our sql component. Notice that we use a direct endpoint in front of the sql endpoint. This allows us to send an exchange to the direct endpoint with the URI, direct:simple, which is much easier for the client to use than the long sql: URI. Note that the DataSource is looked up up in the registry, so we can use standard Spring XML to configure our DataSource. from("direct:simple") .to("sql:select * from projects where license = # order by id?dataSourceRef=jdbc/myDataSource") .to("mock:result"); And then we fire the message into the direct endpoint that will route it to our sql component that queries the database. MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); // send the query to direct that will route it to the sql where we will execute the query // and bind the parameters with the data from the body. The body only contains one value // in this case (XXX) but if we should use multi values then the body will be iterated // so we could supply a List<String> instead containing each binding value. template.sendBody("direct:simple", "XXX"); mock.assertIsSatisfied(); // the result is a List List received = assertIsInstanceOf(List.class, mock.getReceivedExchanges().get(0).getIn().getBody()); // and each row in the list is a Map Map row = assertIsInstanceOf(Map.class, received.get(0)); // and we should be able the get the project from the map that should be Linux assertEquals("Linux", row.get("PROJECT")); We could configure the DataSource in Spring XML as follows:
<jee:jndi-lookup id="myDS" jndi-name="jdbc/myDataSource"/>
See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > SQL Component confluence
- [CONF] Apache Camel > SQL Component confluence