Fast and Flexible CSV Parsing with Univocity Parsers

Introduction to Univocity Parsers in the Spring Boot Demo Project

street night light
Unsplash. Photo by Per Guillaume Gellet

Univocity Parsers is a file parser library written in Java. It is incredibly performant and provides a convenient interface for data handling. According to statistics, this is the fastest CSV parser:

Source from https://github.com/uniVocity/csv-parsers-comparison

It’s convenient to use a library instead of building your own custom parser. But this library also serves as a solid framework for developing new parsers.

In this tutorial, I will show you how to use the Univocity Parsers library to retrieve information from a CSV file. I will demonstrate usage in a Spring Boot project with a simple yet practical use case.

let’s get started!

As a developer, it is a common task to parse CSV files and perform various operations based on the data output. Let’s say you have several service configuration properties stored in a CSV file. As an admin user, you want to retrieve service data through a REST endpoint.

Here is the flow overview:

Demo App Flow Overview

add dependency

I have created a simple sprint boot project via https://start.spring.io/. Let’s add dependencies for this demo:

create data

Let’s create a file named data.csv With the following comma-separated records:

SERVICE_ID,SERVICE_NAME,SERVICE_CASE,SERVICE_CLIENT
1,SERVICE1,TEST1,CLIENT1
2,SERVICE2,TEST2,CLIENT2
12,SERVICE12,TEST12,CLIENT21

This file will act like a database, and we will query its information.

Note that the library can also parse files in different formats such as TSV.

create dto class

Let’s create a Java class that holds the data. Here’s what it looks like:

  • @Data Lombok annotations eliminate boilerplate code (getters, setters, etc.).
  • @Parsed Similar to Annotations from UniVocity @Column Annotations from Java Persistence. This matches the column name of our CSV file.

add service

Let’s add a service to handle records:

It looks like querying data from database using Spring JPA. But we don’t want to read the CSV file every time the request is received. This service will work like a cache.

  • we populate dataMap With data from a CSV file. This is our cash.
  • @PostConstruct The annotation guarantees that the bean will be fully initialized, and we can use the required dependencies.
  • parseDataFromFile method reads from file resources folder.
  • we define a CsvParserSettings configuration class for CsvParser,
  • We a. add up BeanListProcessor which stores all the Java objects generated from the parsed input in a list. In this way, we will easily access the data using Getters,
  • Note we have also instructed CsvParserSettings To select particular fields using selectFields way. This means we won’t load fields we don’t need. It also speeds up the process.
  • getDataById And getAllData The methods are similar to JPA repository methods.

add rest controller

Now, add a Rest Controller to retrieve the data:

We will use GET requests to retrieve the data accordingly.

test the application

It’s test time!

In a terminal or in your browser, execute the following GET request:

http://localhost:8080/getAll

You should see a response like this:

service response

note that serviceClient Ignored as expected.

Let’s try to retrieve service name by id:

GET http://localhost:8080/getServiceCaseById?id=2

answer:

TEST2

Great! It worked as expected.

In this tutorial, you learned how to read CSV files and manipulate data with ease using the Univocity Parsers Java library.

The library is robust and provides more options like CSV writing, custom parser settings, etc. To learn more about all supported functions, visit the documentation.

The source code for this demo can be found in my GitHub repository.

If you enjoyed this post, you might also like my other Spring Boot/CSV-related articles:

Thanks for reading and happy coding!

Leave a Comment