Tuesday 8 January 2019

Camel: Transform the message using Processor API


In this post, I am going to explain, how to transform the message using processor API.

Scenario
Source folder ‘jsonOrders’ has all the orders information in json files format. We need to use camel transformation to convert these json orders into csv format and save it in another folder ‘csvOrders’.

jsonOrder contain two orders order1.json, order2.json.

order1.json
{
 "orderId" : "Ord11279327",
 "productId" : "Prod342",
 "noOfProducts" : 5
}


order2.json
{
 "orderId" : "Ord19897",
 "productId" : "Prod76",
 "noOfProducts" : 1
}

Our task is to read the orders from jsonOrders folder and convert them to csv format and place it in csvOrders folder.

You can do this by writing a custom processor that converts the json data to csv.

public class JsonToCsvTranslator implements Processor {
         private static Gson gson = new Gson();

         @Override
         public void process(Exchange exchange) throws Exception {

                  String jsonData = exchange.getIn().getBody(String.class);
                  String fileName = (String) exchange.getIn().getHeader("CamelFileName");

                  Order order = gson.fromJson(jsonData, Order.class);

                  String csvData = order.getOrderId() + "," + order.getProductId() + "," + order.getNoOfProducts();
                  String csvFileName = fileName.substring(0, fileName.indexOf(".json")) + ".csv";

                  System.out.println("Data from " + fileName + " is transformed to " + csvData);
                  System.out.println(fileName + " is transfomed to " + csvFileName);

                  exchange.getIn().setBody(csvData);
                  exchange.getIn().setHeader("CamelFileName", csvFileName);
         }

}

Use the JsonToCsvTranslator to convert json data to csv.

from("file:C:\\Users\\Public\\jsonOrders?noop=true")
         .process(new JsonToCsvTranslator())
         .to("file:C:\\Users\\Public\\csvOrders");



Step 1: Create new maven project ‘camelMessageTransformation’.

Step 2: Open pom.xml and update maven dependencies
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>camelMessageTransformation</groupId>
 <artifactId>camelMessageTransformation</artifactId>
 <version>1</version>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
   <version>2.22.1</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  <dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.5</version>
  </dependency>


 </dependencies>
</project>

Step 3: Create a package ‘com.sample.app.model’ and define Order class.


Order.java
package com.sample.model;

public class Order {
 private String orderId;
 private String productId;
 private int noOfProducts;

 public String getOrderId() {
  return orderId;
 }

 public void setOrderId(String orderId) {
  this.orderId = orderId;
 }

 public String getProductId() {
  return productId;
 }

 public void setProductId(String productId) {
  this.productId = productId;
 }

 public int getNoOfProducts() {
  return noOfProducts;
 }

 public void setNoOfProducts(int noOfProducts) {
  this.noOfProducts = noOfProducts;
 }

}

Step 4: Create a package 'com.sample.app.translators' and define the class JsonToCsvTranslator.


JsonToCsvTranslator.java
package com.sample.app.translators;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import com.google.gson.Gson;
import com.sample.model.Order;

public class JsonToCsvTranslator implements Processor {
 private static Gson gson = new Gson();

 @Override
 public void process(Exchange exchange) throws Exception {

  String jsonData = exchange.getIn().getBody(String.class);
  String fileName = (String) exchange.getIn().getHeader("CamelFileName");

  Order order = gson.fromJson(jsonData, Order.class);

  String csvData = order.getOrderId() + "," + order.getProductId() + "," + order.getNoOfProducts();
  String csvFileName = fileName.substring(0, fileName.indexOf(".json")) + ".csv";

  System.out.println("Data from " + fileName + " is transformed to " + csvData);
  System.out.println(fileName + " is transfomed to " + csvFileName);

  exchange.getIn().setBody(csvData);
  exchange.getIn().setHeader("CamelFileName", csvFileName);
 }

}

Step 5: Create a package ‘com.sample.app.routes’ and define the class ‘FileCopyRoute.java’.


FileCopyRoute.java
package com.sample.app.routes;

import org.apache.camel.builder.RouteBuilder;

import com.sample.app.translators.JsonToCsvTranslator;

public class FileCopyRoute extends RouteBuilder {

 @Override
 public void configure() throws Exception {
  from("file:C:\\Users\\Public\\jsonOrders?noop=true").process(new JsonToCsvTranslator())
    .to("file:C:\\Users\\Public\\csvOrders");
 }

}

Step 6: Create a package ‘com.sample.app’ and define the class Application.java.


Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

import com.sample.app.routes.FileCopyRoute;

public class Application {
 
 public static void main(String args[]) throws Exception {
  CamelContext context = new DefaultCamelContext();

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }

}

Run Application.java, you can see below messages in console.

Data from order1.json is transformed to Ord11279327,Prod342,5
order1.json is transfomed to order1.csv
Data from order2.json is transformed to Ord19897,Prod76,1
order2.json is transfomed to order2.csv


Complete project structure looks like below.




Previous                                                 Next                                                 Home<

No comments:

Post a Comment