简体   繁体   中英

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

I am new to Apache Camel. I have the below XML,which I am consuming from a restful api. I have used JaxB to generate four objects for it. ie ConsumerList.java, Consumer.java, Address.java using 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>

Now my requirement is to save those 4 objects to DB. Below is my route from 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>

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.

Add the below beans to your SpringConfig.xml file

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

Then use this in CamelRouter.java by pass this to transform body

This .transform().body() will set these HashMap values to your Query parameters.

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)

Map should contain :

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

Having value for sample_val1 set in Map

Note : Query should contain # for the values which should be replaced from HashMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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