如何使用apache camel从mysql表读取数据并写入另一个表 [英] How to use apache camel to read data from mysql table and write into another table
问题描述
伙计们,我正在使用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;
}
}
伙计们,我不确定应该在至"端点中给出哪个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-jdb C组件需要SQL文本,因此主体应包含一个插入语句...
the camel-jdbc component expects SQL text, so the body should contain an insert statement...
因此,您需要解析来自选择的stmt的结果,该结果返回ArrayList<HashMap<String, Object>>
... split()将您带到HashMap<String, Object>
,因此您可以使用
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...
类似这样的东西...
something like this...
.setBody(simple("insert into employee values('${body[id]','${body[name]}')"))
这篇关于如何使用apache camel从mysql表读取数据并写入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!