Searching...
Sunday, 13 October 2013

Camel Transformation using Transform Example

11:00
In the previous example we transformed data using Processor and update the exchange object. Transform()is a method in the Java DSL that can be used in Camel routes to trans-form messages. It is allowing the use of expressions. In this example we will create a Simple Router and implementing transform method to do the transformation.

Goal-


Transform text file to csv file using Camel transform()

1. Create a Maven Project and add below dependencies


<dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.12.1</version>
        </dependency>


1. Create a Camel Route and start


To create Camel router we need to extends RouteBuilder class and implement configure() method, here we are defining two end points like source and destination folder. While running this router it will consume all the files from the source folder and transform method will replace all line separated symbol to comma and copying to destination folder.
package com.vinod.test;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class CamelTransformExample {
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.addRouteBuilder(new MyTransRoute());
        main.run(args);
    }
}

class MyTransRoute extends RouteBuilder {
    public void configure() {
        System.out.println("My Routing Started");
        from("file:source?noop=true").transform(body().regexReplaceAll(System.getProperty("line.separator"), ","))
                .to("file:destination?fileName=output.csv");
        System.out.println("My Routing complete");
    }
}

 

2. Run the program


Run the program and place the input file in the source folder

0 comments: