Search in sources :

Example 26 with SchemaNotFoundException

use of org.apache.nifi.schema.access.SchemaNotFoundException in project nifi by apache.

the class TestCSVHeaderSchemaStrategy method testSimple.

@Test
public void testSimple() throws SchemaNotFoundException, IOException {
    final String headerLine = "a, b, c, d, e\\,z, f";
    final byte[] headerBytes = headerLine.getBytes();
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(CSVUtils.CSV_FORMAT, CSVUtils.CUSTOM.getValue());
    properties.put(CSVUtils.COMMENT_MARKER, "#");
    properties.put(CSVUtils.VALUE_SEPARATOR, ",");
    properties.put(CSVUtils.TRIM_FIELDS, "true");
    properties.put(CSVUtils.QUOTE_CHAR, "\"");
    properties.put(CSVUtils.ESCAPE_CHAR, "\\");
    final ConfigurationContext context = new MockConfigurationContext(properties, null);
    final CSVHeaderSchemaStrategy strategy = new CSVHeaderSchemaStrategy(context);
    final RecordSchema schema;
    try (final InputStream bais = new ByteArrayInputStream(headerBytes)) {
        schema = strategy.getSchema(null, bais, null);
    }
    final List<String> expectedFieldNames = Arrays.asList("a", "b", "c", "d", "e,z", "f");
    assertEquals(expectedFieldNames, schema.getFieldNames());
    assertTrue(schema.getFields().stream().allMatch(field -> field.getDataType().equals(RecordFieldType.STRING.getDataType())));
}
Also used : MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Arrays(java.util.Arrays) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) List(java.util.List) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Aggregations

SchemaNotFoundException (org.apache.nifi.schema.access.SchemaNotFoundException)26 IOException (java.io.IOException)19 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)19 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)13 InputStream (java.io.InputStream)12 RecordReader (org.apache.nifi.serialization.RecordReader)12 RecordReaderFactory (org.apache.nifi.serialization.RecordReaderFactory)12 FlowFile (org.apache.nifi.flowfile.FlowFile)11 ProcessException (org.apache.nifi.processor.exception.ProcessException)11 RecordSetWriterFactory (org.apache.nifi.serialization.RecordSetWriterFactory)10 Record (org.apache.nifi.serialization.record.Record)9 RecordSetWriter (org.apache.nifi.serialization.RecordSetWriter)8 OutputStream (java.io.OutputStream)7 Map (java.util.Map)7 HashMap (java.util.HashMap)6 WriteResult (org.apache.nifi.serialization.WriteResult)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4 BufferedInputStream (java.io.BufferedInputStream)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3