mirror of https://github.com/apache/nifi.git
NIFI-4966 - JacksonCSVRecordReader - NPE with some CSV formats
Signed-off-by: Matthew Burgess <mattyb149@apache.org> This closes #2535
This commit is contained in:
parent
29c2e18832
commit
c75604a564
|
@ -103,7 +103,7 @@ public class JacksonCSVRecordReader implements RecordReader {
|
||||||
CsvSchema csvSchema = csvSchemaBuilder.build();
|
CsvSchema csvSchema = csvSchemaBuilder.build();
|
||||||
|
|
||||||
// Add remaining config options to the mapper
|
// Add remaining config options to the mapper
|
||||||
List<CsvParser.Feature> features = new ArrayList<>(3);
|
List<CsvParser.Feature> features = new ArrayList<>();
|
||||||
features.add(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS);
|
features.add(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS);
|
||||||
if (csvFormat.getIgnoreEmptyLines()) {
|
if (csvFormat.getIgnoreEmptyLines()) {
|
||||||
features.add(CsvParser.Feature.SKIP_EMPTY_LINES);
|
features.add(CsvParser.Feature.SKIP_EMPTY_LINES);
|
||||||
|
@ -114,7 +114,7 @@ public class JacksonCSVRecordReader implements RecordReader {
|
||||||
|
|
||||||
ObjectReader objReader = mapper.readerFor(String[].class)
|
ObjectReader objReader = mapper.readerFor(String[].class)
|
||||||
.with(csvSchema)
|
.with(csvSchema)
|
||||||
.withFeatures(features.toArray(new CsvParser.Feature[3]));
|
.withFeatures(features.toArray(new CsvParser.Feature[features.size()]));
|
||||||
|
|
||||||
recordStream = objReader.readValues(reader);
|
recordStream = objReader.readValues(reader);
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,6 +123,29 @@ public class TestJacksonCSVRecordReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExcelFormat() throws IOException, MalformedRecordException {
|
||||||
|
final List<RecordField> fields = new ArrayList<RecordField>();
|
||||||
|
fields.add(new RecordField("fieldA", RecordFieldType.STRING.getDataType()));
|
||||||
|
fields.add(new RecordField("fieldB", RecordFieldType.STRING.getDataType()));
|
||||||
|
final RecordSchema schema = new SimpleRecordSchema(fields);
|
||||||
|
|
||||||
|
final String headerLine = "fieldA,fieldB";
|
||||||
|
final String inputRecord = "valueA,valueB";
|
||||||
|
final String csvData = headerLine + "\n" + inputRecord;
|
||||||
|
final byte[] inputData = csvData.getBytes();
|
||||||
|
|
||||||
|
try (final InputStream bais = new ByteArrayInputStream(inputData);
|
||||||
|
final JacksonCSVRecordReader reader = createReader(bais, schema, CSVFormat.EXCEL)) {
|
||||||
|
|
||||||
|
final Object[] record = reader.nextRecord().getValues();
|
||||||
|
final Object[] expectedValues = new Object[] {"valueA", "valueB"};
|
||||||
|
Assert.assertArrayEquals(expectedValues, record);
|
||||||
|
|
||||||
|
assertNull(reader.nextRecord());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultipleRecords() throws IOException, MalformedRecordException {
|
public void testMultipleRecords() throws IOException, MalformedRecordException {
|
||||||
final List<RecordField> fields = getDefaultFields();
|
final List<RecordField> fields = getDefaultFields();
|
||||||
|
|
Loading…
Reference in New Issue