Member-only story

Using Bindy to parse CSV files

Manav Phull
3 min readJan 13, 2023

--

Camel Bindy is a data format binding framework that allows for the conversion of flat files (such as CSV, fixed-length, or custom-delimited files) into Java objects and vice versa. It is built on top of the Apache Camel framework, which provides routing and mediation for various data formats and protocols.

Photo by Sigmund on Unsplash

One of the main advantages of using Camel Bindy is that it allows for easy and efficient processing of large flat files. It also allows for flexibility in handling different data formats and customizing the mapping between the flat file and the Java object.

In this blog post, we will look at parsing and processing CSV files using camel bindy. The same process can be used for fixed-length and other delimited files as well

The CSV File
Let’s create a simple CSV file having a person’s name, address and phone# as below.

John,12 Toronto St,418384943
Smith,34 Madison Dr,484584505
Martha,99 Dover Cres,393484304

The POJO Class
Next, let’s take our stater spring boot project and add a POJO to it representing this CSV format

import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;

@CsvRecord(separator = ",")
public class Person {

@DataField(pos = 1)
private String name;
@DataField(pos = 2)
private String address;
@DataField(pos = 3)
private String phone;

... add constructors, getters and setters

Let’s dissect this class a bit

  • The @CsvRecord annotation indicates that this POJO is representing a CSV record
  • The separator is denoted as , for a comma-separated file indicating the delimiter to use for unmarshalling of a record. For a pipe delimited file, we can change it to | .A few things to note about separator,
  • The only whitespace character supported as separator is \t
  • If we want to marshal() a file from a POJO, we need to identify the separator as a quote i.e.
@CsvRecord(separator = ",", quote = ",")
public class Person {
  • If the CSV file contains a header, then we can skip the header row by using the `skipFirstLine` attribute like…

--

--

Manav Phull
Manav Phull

Written by Manav Phull

Problem Solver | Driving innovation at the intersection of finance and tech💡 #Fintech #Leadership #TechStrategy #CapitalMarkets #WealthTech #Innovation

No responses yet

Write a response