Repository: nifi
Updated Branches:
  refs/heads/master c75604a56 -> 95e543d4c


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

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

This closes #2536


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/95e543d4
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/95e543d4
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/95e543d4

Branch: refs/heads/master
Commit: 95e543d4cce9148c91898827f15c2c61d3565265
Parents: c75604a
Author: Pierre Villard <pierre.villard...@gmail.com>
Authored: Mon Mar 12 23:44:06 2018 +0100
Committer: Matthew Burgess <mattyb...@apache.org>
Committed: Tue Mar 13 16:54:46 2018 -0400

----------------------------------------------------------------------
 .../org/apache/nifi/csv/CSVRecordReader.java    |  2 ++
 .../apache/nifi/csv/TestCSVRecordReader.java    | 23 ++++++++++++++++++++
 2 files changed, 25 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/95e543d4/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
index f01fc3e..20a0788 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
@@ -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]));

http://git-wip-us.apache.org/repos/asf/nifi/blob/95e543d4/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestCSVRecordReader.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestCSVRecordReader.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestCSVRecordReader.java
index 7920a33..d0fca7d 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestCSVRecordReader.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestCSVRecordReader.java
@@ -125,6 +125,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();
         fields.replaceAll(f -> f.getFieldName().equals("balance") ? new 
RecordField("balance", doubleDataType) : f);

Reply via email to