-
Notifications
You must be signed in to change notification settings - Fork 76
CsvSchema
Most aspects of CSV parsing and generation are controlled by CsvSchema
associated with the reader/parser and writer/generator. Schema defines two main kinds of things:
- Columns that the CSV document has, to allow for binding logical property name to/from actual physical column in CSV document (by index)
- Details of how contents within columns (cell values) are encoded
There are 3 ways to construct a CsvSchema
instance with column mapping definition (and one way for "unmapped" reading):
- Create schema based on a Java class
- Build schema programmatically (explicit code)
- Use the first line of CSV document to get the names (no types) for Schema
(TO BE WRITTEN)
(TO BE WRITTEN)
(TO BE WRITTEN)
The way to construct an "unmapped" schema is to:
CsvSchema unmapped = CsvSchema.emptySchema();
but this "schema" is also the default one configured to ObjectReader
s and ObjectWriter
s if you do not explicitly specify a schema. So it is possible to simply omit the definition and read content like:
CsvMapper mapper = new CsvMapper();
mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
// when wrapped as an array, we'll get array/List of arrays/Lists, for example:
String[][] rows = mapper.readValue(csvContent, String[][].class);
You can also use streaming read like so:
// NOTE: type given for 'readerFor' is type of individual row
MappingIterator<String[]> it = mapper.readerFor(String[].class)
.readValues(csvSource);
while (it.hasNextValue()) {
String[] row = it.nextValue();
Note that when using "unmapped" schema, content must be read as arrays/Lists of String or java.lang.Object
s (String[]
, List<String>
etc); no other types are accepted
TO BE COMPLETED