NIFI-4955 - Preserve columns ordering with CSV and ValidateRecord

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

This closes #2528
This commit is contained in:
Pierre Villard 2018-03-09 18:49:27 +01:00 committed by Matthew Burgess
parent 95e543d4cc
commit 11a7d5c09f
2 changed files with 64 additions and 2 deletions

View File

@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.processors.standard;
import org.apache.nifi.csv.CSVReader;
import org.apache.nifi.csv.CSVRecordSetWriter;
import org.apache.nifi.csv.CSVUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Before;
import org.junit.Test;
public class TestValidateRecord {
private TestRunner runner;
@Before
public void setup() throws InitializationException {
runner = TestRunners.newTestRunner(ValidateRecord.class);
}
@Test
public void testColumnsOrder() throws InitializationException {
final CSVReader csvReader = new CSVReader();
runner.addControllerService("reader", csvReader);
runner.setProperty(csvReader, CSVUtils.FIRST_LINE_IS_HEADER, "true");
runner.setProperty(csvReader, CSVUtils.QUOTE_MODE, CSVUtils.QUOTE_MINIMAL.getValue());
runner.setProperty(csvReader, CSVUtils.TRAILING_DELIMITER, "false");
runner.enableControllerService(csvReader);
final CSVRecordSetWriter csvWriter = new CSVRecordSetWriter();
runner.addControllerService("writer", csvWriter);
runner.setProperty(csvWriter, "Schema Write Strategy", "full-schema-attribute");
runner.enableControllerService(csvWriter);
runner.setProperty(ValidateRecord.RECORD_READER, "reader");
runner.setProperty(ValidateRecord.RECORD_WRITER, "writer");
final String content = "fieldA,fieldB,fieldC,fieldD,fieldE,fieldF\nvalueA,valueB,valueC,valueD,valueE,valueF\nvalueA,valueB,valueC,valueD,valueE,valueF\n";
runner.enqueue(content);
runner.run();
runner.assertAllFlowFilesTransferred(ValidateRecord.REL_VALID, 1);
runner.getFlowFilesForRelationship(ValidateRecord.REL_VALID).get(0).assertContentEquals(content);
}
}

View File

@ -23,7 +23,7 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -95,7 +95,7 @@ public class CSVRecordReader implements RecordReader {
final int numFieldNames = recordFields.size(); final int numFieldNames = recordFields.size();
for (final CSVRecord csvRecord : csvParser) { for (final CSVRecord csvRecord : csvParser) {
final Map<String, Object> values = new HashMap<>(recordFields.size() * 2); final Map<String, Object> values = new LinkedHashMap<>(recordFields.size() * 2);
for (int i = 0; i < csvRecord.size(); i++) { for (int i = 0; i < csvRecord.size(); i++) {
final String rawValue = csvRecord.get(i); final String rawValue = csvRecord.get(i);