Skip to content

Instantly share code, notes, and snippets.

@asafary
Last active April 30, 2024 11:36
Show Gist options
  • Save asafary/9675488 to your computer and use it in GitHub Desktop.
Save asafary/9675488 to your computer and use it in GitHub Desktop.
Example of how to read a Pojo from a CSV file using Jackson JSON Processor
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.3.0'
testCompile 'junit:junit:4.11'
}
Tom Tommy 32 m
Anna Anny 27 f
package org.asafary.csv;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonPropertyOrder({ "name", "surname", "shoesize", "gender" })
public class Person {
public String name;
public String surname;
public int shoesize;
public String gender;
}
package org.asafary.csv;
import java.io.File;
import java.util.List;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
/**
* Reads Person objects from a CSV file
*/
public class PersonReader {
public static List<Person> readFile(File csvFile) throws Exception {
MappingIterator<Person> personIter = new CsvMapper().readerWithTypedSchemaFor(Person.class).readValues(csvFile);
return personIter.readAll();
}
}
package org.asafary.csv;
import static org.junit.Assert.*;
import java.io.File;
import java.util.List;
import org.junit.Test;
public class PersonReaderTest {
@Test
public void testReadingPersonObjectsFromCsvData() throws Exception {
File testFile = new File("people.csv");
List<Person> people = PersonReader.readFile(testFile);
assertEquals(2, people.size());
Person person1 = people.get(0);
assertEquals("Tom", person1.getName());
assertEquals("Tommy", person1.getSurname());
assertEquals(32, person1.getShoesize());
assertEquals("m", person1.getGender());
Person person2 = people.get(1);
assertEquals("Anna", person2.getName());
assertEquals("Anny", person2.getSurname());
assertEquals(27, person2.getShoesize());
assertEquals("f", person2.getGender());
}
}
@monvogul
Copy link

Great!!

@adbhutrai
Copy link

Hi,

Thanks for posting this, informative and explainatory.

Is there a way to skip the header with this approach?

I will appreciate your quick help.

Thanks

@praneetmehta
Copy link

MappingIterator personIter = new CsvMapper().readerWithTypedSchemaFor(Person.class).withoutHeader().readValues(csvFile);

@LangInteger
Copy link

LangInteger commented Aug 19, 2019

To skip header

      CsvMapper mapper = new CsvMapper();

      CsvSchema sclema = mapper.schemaFor(XX.class)
          .withSkipFirstDataRow(true)
          .withColumnSeparator('|').withoutQuoteChar();

      MappingIterator<XX> iterator = mapper
          .readerFor(XX.class)
          .with(sclema).readValues(file);

      List<XX> hotelSummaries = iterator.readAll();

@PraveenBeedanal
Copy link

I tried this approach, but values are mapping randomly to the POJO.There is no order

@daniel94104
Copy link

I like this approach, clean and generic. Thumb up

@sumangoudk
Copy link

For this, the POJO should entirely match with CSV in the same order. other wise the data will be mapped randomly.

@srinu-tippana
Copy link

How to skip we have multiple headers on the top and also a trailer at the bottom using this approach

@kathyrollo
Copy link

How to skip we have multiple headers on the top and also a trailer at the bottom using this approach

JUnit 5 already has this feature out of the box.

@srinu-tippana
Copy link

Junit will fall under only for to test right .I want it on dev basis

@Kazitanvirazad
Copy link

Kazitanvirazad commented Apr 30, 2024

Parsing csv file and mapping it with Java classes is sometimes a very hectic work. There are many csv parser libraries available which makes our lives easy but still they can create complexity in implementing them in our existing codebase. To resolve all these issues and make the parsing process more simpler we can use a new csv parsing library called csv4pojo. csv4pojo makes its implementation in our existing code simpler and its a very lightweight Java library and processes the parsing very fast. It is capable of parsing millions of rows of data and can write same amount data in the csv file. It supports 15 different datatypes. Integer,String,Float,Double,Boolean,Long,Character,Class,Integer[],String[],Float[],Double[],Boolean[],Character[]Long[].

org.csv4pojoparser csv4pojo 2.0.0 Add this maven dependencies in your pom.xml and read the docs - https://github.com/Kazitanvirazad/CSV4POJO/blob/master/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment