-
-
Save asafary/9675488 to your computer and use it in GitHub Desktop.
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()); | |
} | |
} |
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
MappingIterator personIter = new CsvMapper().readerWithTypedSchemaFor(Person.class).withoutHeader().readValues(csvFile);
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();
I tried this approach, but values are mapping randomly to the POJO.There is no order
I like this approach, clean and generic. Thumb up
For this, the POJO should entirely match with CSV in the same order. other wise the data will be mapped randomly.
How to skip we have multiple headers on the top and also a trailer at the bottom using this approach
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.
Junit will fall under only for to test right .I want it on dev basis
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
Great!!