【问题标题】:How to extract data from xml and persist it to DB using Apache Camel如何使用 Apache Camel 从 xml 中提取数据并将其保存到 DB
【发布时间】:2016-03-04 08:16:12
【问题描述】:

我是 Apache Camel 的新手。我有下面的 XML,我从一个安静的 api 中使用它。我已经使用 JaxB 为其生成了四个对象。即 ConsumerList.java、Consumer.java、Address.java 使用 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 个对象保存到数据库。以下是我从 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&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>

我已经解组了这些对象,但我不知道如何将其插入数据库。

【问题讨论】:

  • 您可以从 Camel 端点 SQLJDBC 的文档开始。
  • 我看过那些。我唯一的问题是我不确定应该在插入语句中输入哪些值。我的意思是 xml 中的地址字段具有街道和城市属性。相同的地址类由 jaxb 创建。现在我如何获取街道和城市属性并放入插入语句。
  • 这里有很多数据库例子你可以先学习一下:github.com/apache/camel/tree/master/examples

标签: java xml jaxb apache-camel


【解决方案1】:

使用您的正常插入/更新查询,使用 apache camel 将您的数据持久保存到数据库中。

将以下 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>

根据插入查询中提到的名称设置已解组到哈希图中的值,并在 MessageProcessor 中将其设置为 exchange.getOut().setBody班级。

然后在 CamelRouter.java 中使用它,将它传递给 transform body

.transform().body() 会将这些 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 替换的值

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多