Reputation: 1500
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;
}
}
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
Upvotes: 4
Views: 7992
Reputation: 21015
the camel-jdbc component expects SQL text, so the body should contain an insert statement...
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]}')"))
Upvotes: 2