How to use apache camel to read data from mysql table and write into another table(如何使用apache camel从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...
所以,您需要从返回 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]}')"))
这篇关于如何使用apache camel从mysql表中读取数据并写入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用apache camel从mysql表中读取数据并写入另一个表


- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01