如何使用apachecamel从mysql表中读取数据并写入另一个表 [英] How to use apache camel to read data from mysql table and write into another table

查看:54
本文介绍了如何使用apachecamel从mysql表中读取数据并写入另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我正在使用 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...

因此,您需要解析选择 stmt 的结果,该结果返回 ArrayList...split() 将您带到 HashMap,因此您可以使用 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]}')"))

这篇关于如何使用apachecamel从mysql表中读取数据并写入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆