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