繁体   English   中英

如何使用Apache Camel从xml提取数据并将其持久保存到DB

[英]How to extract data from xml and persist it to DB using Apache Camel

我是Apache Camel的新手。 我有下面的XML,我从一个宁静的api中使用。 我已经使用JaxB为其生成了四个对象。 即使用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>

现在,我的要求是将这4个对象保存到DB。 以下是我从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>

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

使用常规的插入/更新查询,使用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()会将这些HashMap值设置为您的Query参数。

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替换的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM