NIFI-4967 - CSVRecordReader does not read header with specific formats

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #2536
This commit is contained in:
Pierre Villard 2018-03-12 23:44:06 +01:00 committed by Matthew Burgess
parent c75604a564
commit 95e543d4cc
2 changed files with 25 additions and 0 deletions

View File

@ -77,6 +77,8 @@ public class CSVRecordReader implements RecordReader {
if (ignoreHeader) {
withHeader = withHeader.withHeader(schema.getFieldNames().toArray(new String[0]));
} else {
withHeader = withHeader.withFirstRecordAsHeader();
}
} else {
withHeader = csvFormat.withHeader(schema.getFieldNames().toArray(new String[0]));

View File

@ -124,6 +124,29 @@ public class TestCSVRecordReader {
}
}
@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 CSVRecordReader 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
public void testMultipleRecords() throws IOException, MalformedRecordException {
final List<RecordField> fields = getDefaultFields();