Apache Camel with database query in Java DSL

2.1k views Asked by At

I'm currently trying to import some data from Oracle to ElasticSearch (in JSON format) using Apache Camel. I'm totally new on this framework, so I was thinking that you might help with it !

The problem is that my routeBuilder take the data from my table Entreprise .setBody(constant("select * from entreprise")).to("jdbc:myDataSource") and I get something like that : {id=1231, test=hello}, so I put it in a custom processor which add the quotes. I think this is quite ugly to modify a String because of the integers, booleans, arrays that shouldn't be between quotes, so I'd rather use an Object like a HashMap.

I've tried the apache-sql example which use Spring to create his route :

<route id="processOrder-route">
  <from uri="sql:{{sql.selectOrder}}?consumer.onConsume={{sql.markOrder}}"/>
  <to uri="bean:orderBean?method=processOrder"/>
  <log message="${body}"/>
</route>

and finally get the informations into a HashMap :

 public String processOrder(Map<String, Object> data) {
    return "Processed order id " + data.get("id") + " item " + data.get("item") + " of " + data.get("amount") + " copies of " + data.get("description");
}

So my main question his : what is the equivalent to this route in Java DSL which could return a HashMap or something like that ?

Here is my code :

public class MainApp {
  public static void main(String[] args) throws Exception {

    String url = "jdbc:oracle:thin:alexis/alexis@localhost:1521:xe";
    System.out.println("Setting up data source.");
    DataSource dataSource = setupDataSource(url);
    System.out.println("Done.");

    SimpleRegistry reg = new SimpleRegistry() ;
    reg.put("myDataSource", dataSource);

    CamelContext context = new DefaultCamelContext(reg);
    ProducerTemplate template = context.createProducerTemplate();
    context.addRoutes(new MainApp().new MyRouteBuilder());
    context.start();
    Thread.sleep(3000); 
    context.stop();
  }

  class MyRouteBuilder extends RouteBuilder {

    public void configure() {
        Processor proc = new ConvertToJSON();
        String dst = "C:/Users/**/Desktop/Workspace_sts/democamelJava/data";
        from("timer://foo?period=2s")
        .setBody(constant("select * from entreprise"))
        .to("jdbc:myDataSource").split(body()).process(proc)
        .convertBodyTo(String.class)
        .to("file://" + dst);
        //.to("elasticsearch://localhost:9200?operation=INDEX&indexName=twitter&indexType=tweet");
    }
}

Thank you for every answer !

0

There are 0 answers