[英]How to extract data from xml and persist it to DB using Apache Camel
I am new to Apache Camel. 我是Apache Camel的新手。 I have the below XML,which I am consuming from a restful api. 我有下面的XML,我从一个宁静的api中使用。 I have used JaxB to generate four objects for it. 我已经使用JaxB为其生成了四个对象。 ie ConsumerList.java, Consumer.java, Address.java using apache camel. 即使用Apache骆驼的ConsumerList.java,Consumer.java,Address.java。
<consumer_list>
<consumer>
<name>John</name>
<address>
<street>13 B</street>
<city>Mumbai</city>
</address>
</consumer>
<consumer>
<name>Paul</name>
<address>
<street>82 A</street>
<city>Delhi</city>
</address>
</consumer>
</consumer_list>
Now my requirement is to save those 4 objects to DB. 现在,我的要求是将这4个对象保存到DB。 Below is my route from camel-context.xml: 以下是我从camel-context.xml出发的路线:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/firstdb"/>
<property name="username" value="postgres"/>
<property name="password" value="xyz"/>
</bean>
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="generateOrder-route">
<from uri="timer://foo?fixedRate=true&period=60000"/>
<setHeader headerName="Exchange.HTTP_QUERY">
<constant>dept=7&name=Johnson&offset=0&limit=200</constant>
</setHeader>
<to uri="http://example.com/ibp/api/v3/business_partners/search"/>
<unmarshal>
<jaxb prettyPrint="true" contextPath="target.jaxb.beans"/>
</unmarshal>
<to ???"/>
</route>
</camelContext>
I have unmarshalled those objects but I have no idea as in how to insert it to database. 我已经将这些对象解组,但是我不知道如何将其插入数据库。
Use your normal insert / update queries to persist your data to the DB using apache camel. 使用常规的插入/更新查询,使用apache camel将数据持久保存到数据库中。
Add the below beans to your SpringConfig.xml file 将以下bean添加到您的SpringConfig.xml文件中
<!-- JDBC data source bean-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="YOUR_CONNECTION_STRING" />
<property name="username" value="YOUR_USERNAME" />
<property name="password" value="YOUR_PASSWORD" />
</bean>
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>
Set your values that you have unmarshalled into a hashmap according to the names mentioned in your insert query and set this as exchange.getOut().setBody in your MessageProcessor Class. 根据插入查询中提到的名称,将您已解组到哈希图中的值进行设置,并将其设置为MessageProcessor类中的exchange.getOut()。setBody 。
Then use this in CamelRouter.java by pass this to transform body 然后在CamelRouter.java中使用它,通过传递它来变换主体
This .transform().body() will set these HashMap values to your Query parameters. 这个.transform()。body()会将这些HashMap值设置为您的Query参数。
from("direct:yourHandleName").to("bean:messsageProcessor?method=setMessageInHashMap")
.transform().body()
.to("sql:{{----YOUR QUERY HERE----}}")
.log("INFORMATION SUCCESSFULLY INSERTED IN DB").end();
Eg. 例如。
If Query Contains : insert into sample_table values (:#sample_val1)
如果查询包含: insert into sample_table values (:#sample_val1)
Map should contain : 地图应包含:
hashMapObj.put("sample_val1","<<<YOUR_VALUE>>>");
exchange.getOut().setBody(hashMapObj);
Having value for sample_val1 set in Map 在地图中设置了sample_val1的值
Note : Query should contain # for the values which should be replaced from HashMap 注意: 查询中应包含#表示应从HashMap替换的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.