如何从xml中提取数据并使用Apache Camel将其保存到数据库

问题描述:

我是Apache Camel的新手。我有下面的XML,我从一个安静的API中消费。我用JaxB为它生成了四个对象。即使用apache骆驼的ConsumerList.java,Consumer.java,Address.java。如何从xml中提取数据并使用Apache Camel将其保存到数据库

<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> 

现在我的要求是将这4个对象保存到数据库。下面是从骆驼的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&amp;period=60000"/> 
       <setHeader headerName="Exchange.HTTP_QUERY"> 
        <constant>dept=7&amp;name=Johnson&amp;offset=0&amp;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> 

我已解组的对象,但我不知道在如何将其插入到数据库中。

+1

您可以从骆驼端点的文档[SQL](https://cwiki.apache.org/confluence/display/CAMEL/SQL)和[JDBC](https://cwiki.apache.org/confluence/display/CAMEL/JDBC )。 – SubOptimal

+0

我见过这些。我唯一的问题是我不确定,我应该在插入语句中放入哪些值。我的意思是xml中的地址字段具有街道和城市属性。相同的地址类由jaxb创建。现在我该如何获取街道和城市属性并将其放入插入语句中。 – Sandy

+1

这里有很多数据库示例,您可以先学习:https://github.com/apache/camel/tree/master/examples –

使用正常的插入/更新查询来使用apache骆驼将数据持久化到数据库。

以下豆添加到您的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> 

设置你已经根据你的插入查询中提到的名字解组到一个HashMap并将其设置为exchange.getOut你的价值观().setBody在您的MessageProcessor类。

然后在CamelRouter.java由通这个使用这个变换身体

.transform()。身体()将设置这些值HashMap的你的查询参数。

from("direct:yourHandleName").to("bean:messsageProcessor?method=setMessageInHashMap") 
.transform().body() 
     .to("sql:{{----YOUR QUERY HERE----}}") 
.log("INFORMATION SUCCESSFULLY INSERTED IN DB").end(); 

例如,

如果查询包含:insert into sample_table values (:#sample_val1)

地图应包含以下内容:

hashMapObj.put("sample_val1","<<<YOUR_VALUE>>>"); 
exchange.getOut().setBody(hashMapObj); 

拥有价值sample_val1在地图集

注:查询应该包含#的值应该是从HashMap中取代