Tuesday, 8 January 2019

Camel: Transform the message using beans


You can transform the message using beans. Define a translator that has static map method, which takes a string as input and transform the input and return the transformed string as output.

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

 public static String map(final String messageBody) {

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

  String csvData = order.getOrderId() + "," + order.getProductId() + "," + order.getNoOfProducts();

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

  return csvData;
 }

}

Use the instance of JsonToCsvTranslator, to transform the data.

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

Find the below working application. Below application converts the json files to csv and copy them tom csvOrders folder.

jsonOrders folder contain two files.


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


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

Create new maven project ‘camelMessageTransformation’

Update pom.xml for 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>

Create a package ‘com.sample.model’ and define Order class like below.


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

}

Create a package ‘com.sample.app.translators’ and define the class JsonToCsvTranslator.


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

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

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

 public static String map(final String messageBody) {

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

  String csvData = order.getOrderId() + "," + order.getProductId() + "," + order.getNoOfProducts();

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

  return csvData;
 }

}

Create a package ‘com.sample.app.routes’ and define the class ‘FileCopyRoute’.


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").bean(new JsonToCsvTranslator())
    .to("file:C:\\Users\\Public\\csvOrders");
 }

}

Define Application.java in com.sample.app.


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 is transformed to Ord11279327,Prod342,5
Data from is transformed to Ord19897,Prod76,1


Previous                                                 Next                                                 Home<

No comments:

Post a Comment