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" 
  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"  />

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");

	public void configure() throws Exception {
		.process(new EmployeeFileProcessor());


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


		} catch (Exception e) {


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 {
		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"?>
	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">





Here is the output of Example.

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

Here is the project details.


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