Storing Data From a CSV File To Database Using Apache Camel

Posted By : Hrishabh Mishra | 20-Aug-2020

apache camel

 

Apache Camel supports different types of data format in which csv data format is one of them. we consume csv files from one endpoint, which is also called route in the application. 

              e.g 

                   from("file: path of file").process(exchange->{

 

                     });

 

 

 

          After consuming the file from the consumer we need to unmarshalling the csv file into java objects. 

For unmarshalling, we need to parse a csv data file.

 

 

 

 Parsing the Csv File:


 

                   Apache Camel has the component, which is known as "Bindy" to parse structured data as well  unstructured data. It also has annotations that help in Binding mapping from java beans. The source that you can bind data to is given below.

 

                   -> Csv Record

                   -> fixed length records.

                   -> unstructured data


 

             By adding maven dependency in the pom.xml file in your project and this automatically pulls the .jar file of bindy library or component.

             

                <dependency>

                    <groupId>org.apache.camel.springboot</groupId>

                    <artifactId>camel-bindy-starter</artifactId>

                    <version>x.x.x</version>

                  <!-- use the same version as your Camel core version -->

                </dependency>  

 

   

 

        Create a Pojo Class that can linked the header of csv file together with one one to many relation. and use the "BindyDataformat" class to configure.

 

                 eg, Csv data-   Names;lastNames;titles 

                                 Hrishabh;Mishra;vice-president

 

                  @Entity   

                  @CsvRecord(separator = ";" ,skipField =true,skipFirstLine = true)

                  @Table(name ="ACM")

                  public class Student implements Serializable {

 

                        @DataField(pos = 1)

                        private String Names;

                        @DataField(pos = 2)

                        private String lastNames;

                        @DataField(pos = 2)

                        private String titles;

 

                        }

 

                -> SkipFirstLine = true means it skip se header of csv file.

                -> skipFiled = true means it skip the extra field in csv file.


 

        Configuration with Apache Camel:-

 

 

 

                   from("file: path of file ")

                   .unmarshal(new BindyCsvDataFormat(Student.class))

                   .process(exchange -> {

 

                    List<Student> data = exchange.getIn().getBody(List.class);

 

                   // "data" is the list of object contain body of csv file with data in that object.

 

                    for (Student line : data) {

 

            

                     studentrepo.save(line);

                     //create a jpa repository interface and use the method .save() to 

                     save data in database.

 

                    }

                    });

 

 

 

      Conclusion:-

 

                      Bindy Component in Apache Camel technology is an excellent way to handle both structured and unstructured csv data files. It parses the data, unmarshal into a Java object. and this java object is required, for saving data into the database.


 

We are an ERP software development company that specializes in building performance-driven ERP applications for varied enterprise requirements. For project-related queries, drop us a line at [email protected].


 

 




 


Please remove URL from text

Comment is required

Sending message..
Connect with us