How to use apache camel to read data from mysql table and write to another table

Guys I am using Apache Camel to read data from mysql table. I am successfully printing it to the console. But as per my requirement, I need to read data from one mysql database and then filter it using some condition and then insert the filtered data into another mysql database table. I am posting my code below ..

public class camelJdbc {

    public static void main(String[] args) throws Exception {
        final String url = "jdbc:mysql://localhost:3306/emp";
        final String url1 = "jdbc:mysql://localhost:3306/emp1";
        DataSource dataSource = setupDataSource(url);
        DataSource dataSource1 = setupDataSource1(url1);

        SimpleRegistry reg = new SimpleRegistry() ;
        reg.put("myDataSource",dataSource);
        reg.put("myDataSource1",dataSource1);

        CamelContext context = new DefaultCamelContext(reg);
        context.addRoutes(new camelJdbc().new MyRouteBuilder());

        context.start();
        Thread.sleep(5000);
        context.stop();
    }

    class MyRouteBuilder extends RouteBuilder {
        public void configure() {
            from("timer://Timer?period=60000")
            .setBody(constant("select * from employee"))
            .to("jdbc:myDataSource")
            .split(body())
            .choice()
            .when(body().convertToString().contains("roll=10"))
            .setBody(constant(///////What SQL command should I write here????/////))
            .to("jdbc:myDataSource1")
            .otherwise()
            .to("stream:out");
        }
    }

    private static DataSource setupDataSource(String connectURI) {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUsername("root");
        ds.setPassword("");
        ds.setUrl(connectURI);
        return ds;
    }

    private static DataSource setupDataSource1(String connectURI1) {
        BasicDataSource ds1 = new BasicDataSource();
        ds1.setDriverClassName("com.mysql.jdbc.Driver");
        ds1.setUsername("root");
        ds1.setPassword("");
        ds1.setUrl(connectURI1);
        return ds1;
    }
}

      

Guys I'm not sure which SQL command should be specified in the "before" endpoint. Also, I wrote this code myself, as I don't get a lot of material on the internet, so I'm not even sure if it's even remotely fixed, or I'm completely out of touch. Please help me figure it out. Thanks to

+3


source to share


1 answer


camel-jdb c component expects SQL text, so body must contain insert statement ...

so you need to parse the results from the selected stmt that returns ArrayList<HashMap<String, Object>>

... split () gets you up to HashMap<String, Object>

, so you can extract those map values ​​using camel-simple ...



something like that...

.setBody(simple("insert into employee values('${body[id]','${body[name]}')"))

      

+2


source







All Articles