2.Camel-Data transformation with BeanIO

BeanIO is an open-source Java framework for marshalling and unmarshalling Java beans from a flat file, stream, or simple String object. In this example, we have flat-file(employee.txt) with comma-separated values. We will convert data into Employees java object.

BeanIO is configured using a mappings XML file where we define the mapping from the flat format to Objects (POJOs).

To use the camel BeanIODataFormat class we need to configure the data format using mapping file and stream name.

Mapping file name is employee_mapping.xml and stream name is employeeStream.

<beanio xmlns="http://www.beanio.org/2012/03" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
  <stream name="employeeStream" format="csv" >
    <group name="employees" class="com.kp.work.vo.Employees" minOccurs="0" maxOccurs="unbounded">
     <record name="employee"  class="com.kp.work.vo.Employee" collection="list"  minOccurs="1" maxOccurs="unbounded">
      <field name="firstName"/>
      <field name="lastName" />
      <field name="title" />
      <field name="salary"/>
      <field name="hireDate" format="MMddyyyy"  />
     </record>
    </group>
  </stream>
</beanio>

package com.kp.work.vo;
import java.io.Serializable;
import java.util.List;


public class Employees  implements Serializable 
{
  
	 private static final long serialVersionUID = 1L;
	 private List<Employee> employee = null;

	public List<Employee> getEmployee() {
		return employee;
	}

	public void setEmployee(List<Employee> employee) {
		this.employee = employee;
	}
 
  
}

package com.kp.work.vo;

import java.io.Serializable;
import java.util.Date;
public class Employee  implements Serializable{
 
	private static final long serialVersionUID = 1L;
	
	private String firstName;
	
	private String lastName;
	
	private  String title;
	
	private int salary;
	
	private  Date hireDate;
	
    
    public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public Date getHireDate() {
		return hireDate;
	}
	public void setHireDate(Date hireDate) {
		this.hireDate = hireDate;
	}
	
	
	
    
   
}

package com.kp.work.camel.beanio;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.beanio.BeanIODataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;

public class EmployeeParsingRoute extends RouteBuilder {

	DataFormat employeeDataformat = new BeanIODataFormat("mapping/employee_mapping.xml", "employeeStream");

	@Override
	public void configure() throws Exception {
		from("file:C:/camel-learning/camel-datatransformation-example/employee?noop=true")
		.unmarshal(employeeDataformat)
		.process(new EmployeeFileProcessor());

	}

	public static void main(String[] args) {
		CamelContext camelContext = new DefaultCamelContext();
		try {
			camelContext.addRoutes(new EmployeeParsingRoute());

			camelContext.start();

			Thread.sleep(10000);
			camelContext.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}




package com.kp.work.camel.beanio;

import java.util.ArrayList;

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

import com.kp.work.vo.Employee;
import com.kp.work.vo.Employees;

public class EmployeeFileProcessor implements Processor {
	public void process(Exchange exchange) throws Exception {
		@SuppressWarnings("unchecked")
		ArrayList<Employees> emps = exchange.getIn().getBody(ArrayList.class);
		Employees employees = emps.get(0);
		for(Employee emp : employees.getEmployee()){
			System.out.println("EmployeeName::"+emp.getFirstName()+ " " +emp.getLastName());
		}
		
	}

}

To use BeanIO in Camel routes we need to add camel-beanio dependency in maven file.

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.kp.work.camel</groupId>
	<artifactId>camel-datatransformation-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>camel-poc</name>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<camel-version>2.18.3</camel-version>
	</properties>
	<dependencies>


		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-beanio</artifactId>
			<version>${camel-version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>


		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-all</artifactId>
			<version>1.10.19</version>
			<scope>test</scope>
		</dependency>

	</dependencies>
</project>

Here is the output of Example.

EmployeeName::Joe Smith
EmployeeName::Jane Doe
EmployeeName::Jon Anderson

Here is the project details.

camel-datatransformation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s