[英]Read from database and write to file using camel
I want to read from the database and write the records to a file using Camel. 我想从数据库中读取并使用Camel将记录写入文件。 Below is my code: 以下是我的代码:
import javax.sql.DataSource;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCExampleSimpleRegistry {
public static void main(String[] args) throws Exception {
final String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB";
DataSource dataSource = setupDataSource(url);
SimpleRegistry reg = new SimpleRegistry() ;
reg.put("myDataSource",dataSource);
CamelContext context = new DefaultCamelContext(reg);
context.addRoutes(new JDBCExampleSimpleRegistry().new MyRouteBuilder());
context.start();
Thread.sleep(5000);
context.stop();
}
class MyRouteBuilder extends RouteBuilder {
public void configure() {
String dst = "C:/Local Disk E/TestData/Destination/?fileName=output.txt";
from("direct:myTable")
.setBody(constant("select * from myTable"))
.to("jdbc:myDataSource")
.to("file://" + dst);
}
}
private static DataSource setupDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("sa");
ds.setPassword("devon1");
ds.setUrl(connectURI);
return ds;
}
}
The above program works fine and CamelContext is elegantly shutdown. 上面的程序工作正常,CamelContext优雅地关闭。 However, the target file is not created. 但是,不会创建目标文件。 What am I doing wrong? 我究竟做错了什么?
I am a newbie to Apache Camel so appreciate any help. 我是Apache Camel的新手,所以感谢任何帮助。 I am using JDK7 with Apache Camel 2.12.1 and is not using Spring. 我正在使用JDK7与Apache Camel 2.12.1并且不使用Spring。
You can take a look at the SQL example: http://camel.apache.org/sql-example.html and then to write to file, is just to send to a file instead of calling the bean as the sql example does. 您可以查看SQL示例: http : //camel.apache.org/sql-example.html然后写入文件,只是发送到文件而不是像sql示例那样调用bean。
If you want to use the JDBC component then it doesnt have built-in consumer, so you need to trigger the route using a timer or a quartz scheduler to run the route every X time. 如果您想使用JDBC组件,那么它没有内置消费者,因此您需要使用计时器或石英调度程序来触发路由,以便每隔X时间运行一次路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.