从数据库读取并使用骆驼写入文件 [英] Read from database and write to file using camel

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

问题描述

我想使用Camel从数据库中读取记录并将记录写到文件中.下面是我的代码:

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;
    }
}

上面的程序可以正常工作,并且CamelContext可以正常关闭.但是,未创建目标文件.我在做什么错了?

我是Apache Camel的新手,非常感谢您的帮助.我正在将JDK7与Apache Camel 2.12.1一起使用,而没有使用Spring.

解决方案

您可以看一下SQL示例:

The above program works fine and CamelContext is elegantly shutdown. However, the target file is not created. What am I doing wrong?

I am a newbie to Apache Camel so appreciate any help. I am using JDK7 with Apache Camel 2.12.1 and is not using 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.

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.

这篇关于从数据库读取并使用骆驼写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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