Created
June 18, 2012 18:19
-
-
Save talfco/2949806 to your computer and use it in GitHub Desktop.
CSVTestLoader based on org.supercsv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cloudburo.test; | |
import java.io.FileReader; | |
import java.util.List; | |
import java.util.ArrayList; | |
import org.supercsv.cellprocessor.ift.CellProcessor; | |
import org.supercsv.io.CsvBeanReader; | |
import org.supercsv.io.ICsvBeanReader; | |
import org.supercsv.prefs.CsvPreference; | |
import org.supercsv.cellprocessor.ParseDate; | |
import org.supercsv.cellprocessor.constraint.StrMinMax; | |
import org.supercsv.cellprocessor.constraint.Unique; | |
import com.mongodb.DBObject; | |
public class CSVTestDataLoader { | |
static final CellProcessor[] customerProcessors = new CellProcessor[] { | |
new StrMinMax(1,3), // Expect at least a name with 1-3 characters | |
new StrMinMax(1,3), // Expect at least a sunrmae with 1-3 characters | |
null, | |
null, | |
null, | |
null, | |
null, | |
null, | |
null, | |
new ParseDate("dd.MM.yyyy") // Format of the date is german localization | |
}; | |
public List<DBObject> loadCustomerTestDataSet() throws Exception { | |
List<DBObject> list = new ArrayList<DBObject>(); | |
ICsvBeanReader inFile = new CsvBeanReader(new FileReader("src/com/cloudburo/test/customers.csv"), | |
CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE); | |
final String[] header = inFile.getCSVHeader(true); | |
CustomerDBObject customer; | |
while( (customer = inFile.read(CustomerDBObject.class, header, customerProcessors)) != null) { | |
list.add(customer); | |
} | |
} finally { | |
inFile.close(); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simpe test data loader class which reads the input of a customer.csv file and creates a set of CustomerDBObject (bean class) which can be loaded into a MongoDB.