mirror of https://github.com/apache/nifi.git
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:
parent
95e543d4cc
commit
11a7d5c09f
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -23,7 +23,7 @@ import java.io.InputStreamReader;
|
|||
import java.io.Reader;
|
||||
import java.text.DateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
@ -95,7 +95,7 @@ public class CSVRecordReader implements RecordReader {
|
|||
final int numFieldNames = recordFields.size();
|
||||
|
||||
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++) {
|
||||
final String rawValue = csvRecord.get(i);
|
||||
|
||||
|
|
Loading…
Reference in New Issue