Quarkus Export Data to CSV Example - Apache Commons
Hello everyone, today we will learn how to export and download the data as a CSV file in a Quarkus we application. CSV stands for Comma-Separated-Values and it's a common format for doing a bulk data transfer between systems. For creating and parsing CSV files, we will use Apache Commons' 3rd-party library.
Technologies used:
- Quarkus 2.2.3.final
- Open CSV 1.8
- H2DB
- Hibernate ORM panache
Project directory:
Maven[pom.xml]:
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details utilized by Maven to build the project. It contains default values for most projects. Some of the configurations that can be designated in the POM is the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists, and such can withal be designated.
<?xml version="1.0"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://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>org.knf.dev.demo</groupId> <artifactId>quarkus_export_csv_apache_commons</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <compiler-plugin.version>3.8.1</compiler-plugin.version> <maven.compiler.parameters>true</maven.compiler.parameters> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <quarkus-plugin.version>2.2.3.Final</quarkus-plugin.version> <quarkus.platform.artifact-id>quarkus-universe-bom </quarkus.platform.artifact-id> <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id> <quarkus.platform.version>2.2.3.Final</quarkus.platform.version> <surefire-plugin.version>2.22.1</surefire-plugin.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>${quarkus.platform.artifact-id}</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy-jsonb</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-jdbc-h2</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-hibernate-orm-panache</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>io.quarkus</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus-plugin.version}</version> <executions> <execution> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${compiler-plugin.version}</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <systemProperties><java.util.logging.manager>org.jboss.logmanager.LogManager </java.util.logging.manager> </systemProperties> </configuration> </plugin> </plugins> </build>
<profiles> <profile> <id>native</id> <activation> <property> <name>native</name> </property> </activation> <build> <plugins> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>${surefire-plugin.version}</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <systemProperties><native.image.path>${project.build.directory}/${project.build.finalName}-runner </native.image.path> </systemProperties> </configuration> </execution> </executions> </plugin> </plugins> </build> <properties> <quarkus.package.type>native</quarkus.package.type> </properties> </profile> </profiles></project>
application.properties:
# datasource configurationquarkus.datasource.db-kind = h2quarkus.datasource.username = sa# quarkus.datasource.password =quarkus.datasource.jdbc.url = jdbc:h2:mem:test# drop and create the database at startupquarkus.hibernate-orm.database.generation=drop-and-create
Entity class - Employee.java:
package org.knf.dev.demo.entity;
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;
@Entity@Table(name = "employee")public class Employee { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; private String email; private String country; private int age; private String role;
public Employee() { super(); }
public Employee(String name, String email, String country, int age, String role) { super();
this.name = name; this.email = email; this.country = country; this.age = age; this.role = role; }
public String getRole() { return role; }
public void setRole(String role) { this.role = role; }
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getCountry() { return country; }
public void setCountry(String country) { this.country = country; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }}
DAO layer - EmployeeRepository.java:
package org.knf.dev.demo.repository;
import javax.enterprise.context.ApplicationScoped;import org.knf.dev.demo.entity.Employee;import io.quarkus.hibernate.orm.panache.PanacheRepository;
@ApplicationScopedpublic class EmployeeRepository implements PanacheRepository<Employee> {
}
Service layer - EmployeeService.java:
package org.knf.dev.demo.service;
import java.io.ByteArrayInputStream;import java.util.List;import javax.enterprise.context.ApplicationScoped;import javax.inject.Inject;import javax.transaction.Transactional;import org.knf.dev.demo.entity.Employee;import org.knf.dev.demo.helper.CSVHelper;import org.knf.dev.demo.repository.EmployeeRepository;
@ApplicationScopedpublic class EmployeeService {
@Inject EmployeeRepository employeeRepository;
@Transactional public List<Employee> fetchAll() { dummyDatas(); return employeeRepository.listAll();
}
public ByteArrayInputStream load() { List<Employee> employees = fetchAll();
ByteArrayInputStream in = CSVHelper.employeesToCSV(employees); return in; }
// generate dummy datas public void dummyDatas() { for (int i = 1; i <=20; i++) { employeeRepository.persist( new Employee("sibin-" + i, "mygmail@com-" + i, "USA", 54 + i, "Developer")); } }}
CSV Helper:
package org.knf.dev.demo.helper;
import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.PrintWriter;import java.util.Arrays;import java.util.List;import org.apache.commons.csv.CSVFormat;import org.apache.commons.csv.CSVPrinter;import org.apache.commons.io.output.ByteArrayOutputStream;import org.knf.dev.demo.entity.Employee;
public class CSVHelper {
public static ByteArrayInputStream employeesToCSV(List<Employee> employees) { final CSVFormat format = CSVFormat.DEFAULT. withHeader("ID", "Name", "Email", "Age", "Country", "Role");
try (
ByteArrayOutputStream out = new ByteArrayOutputStream(); CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format);) { for (Employee emp : employees) { List<String> data = Arrays.asList(String. valueOf(emp.getId()),emp.getName(), emp.getEmail(), String.valueOf(emp.getAge()), emp.getCountry(), emp.getRole());
csvPrinter.printRecord(data); }
csvPrinter.flush(); return new ByteArrayInputStream(out.toByteArray()); } catch (IOException e) { throw new RuntimeException("fail to import data to CSV file: " +e.getMessage()); } }}
EmployeeController.java:
package org.knf.dev.demo.controller;
import javax.inject.Inject;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import org.knf.dev.demo.service.EmployeeService;
@Path("/api/export-employees")public class EmployeeController {
@Inject EmployeeService employeeService;
@GET public Response exportCSV() throws Exception {
return Response.ok(employeeService.load(), MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename = employees.csv"). build();
}}
Run & Test the Application:
Hit this URL in your local system, http://localhost:8080/api/export-employees