如何使用apache camel从mysql表中读取数据并写入另一个表

2022-01-19 00:00:00 mysql java apache-camel

伙计们,我正在使用 Apache Camel 从 mysql 表中读取数据.我在控制台上成功打印了它.但是根据我的要求,我需要从一个mysql数据库中读取数据,然后使用一些条件对其进行过滤,然后将过滤后的数据插入另一个mysql数据库表中.我在下面发布我的代码..

Guys I am using Apache Camel to read data from mysql table. I am successfully printing it on console. But according to my requirement I need to read the data from one mysql database, then filter it by using some condition and then insert the filtered data in 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;
    }
}

伙计们,我不确定我应该在to"端点中给出什么 SQL 命令.此外,我自己编写了这段代码,因为我在互联网上没有得到太多的材料,所以我什至不确定它是否完全正确,或者我完全偏离了轨道.请帮我弄清楚.谢谢

Guys I am not sure what SQL command should I give in the "to" endpoint. Also I have written this code by my own as I am not getting much materiel on internet so I am not even sure whether its even remotely correct or I am totally out of track. Kindly help me figure out. Thanks

推荐答案

camel-jdbc 组件需要 SQL 文本,因此主体应包含插入语句...

the camel-jdbc component expects SQL text, so the body should contain an insert statement...

所以,您需要从返回 ArrayList<HashMap<String, Object>>...的 select stmt 解析结果... split() 将您带到 HashMap<String, Object>,因此您可以使用 camel-simple 提取这些地图值..

so, you need to parse the results from your select stmt which returns ArrayList<HashMap<String, Object>>...the split() gets you to HashMap<String, Object>, so you can extract those map values using camel-simple...

这样的……

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

相关文章