Search in sources :

Example 51 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestWriteJsonResult method testMissingAndExtraFieldInWriteRawRecord.

@Test
public void testMissingAndExtraFieldInWriteRawRecord() throws IOException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, null, null, null)) {
        writer.beginRecordSet();
        writer.writeRawRecord(record);
        writer.finishRecordSet();
    }
    final byte[] data = baos.toByteArray();
    final String expected = "[{\"id\":\"1\",\"dob\":\"1/1/1970\"}]";
    final String output = new String(data, StandardCharsets.UTF_8);
    assertEquals(expected, output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) LinkedHashMap(java.util.LinkedHashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 52 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestWriteJsonResult method testMissingFieldInWriteRecord.

@Test
public void testMissingFieldInWriteRecord() throws IOException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, null, null, null)) {
        writer.beginRecordSet();
        writer.writeRecord(record);
        writer.finishRecordSet();
    }
    final byte[] data = baos.toByteArray();
    final String expected = "[{\"id\":\"1\",\"name\":null}]";
    final String output = new String(data, StandardCharsets.UTF_8);
    assertEquals(expected, output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) LinkedHashMap(java.util.LinkedHashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 53 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestWriteJsonResult method testMissingAndExtraFieldInWriteRecord.

@Test
public void testMissingAndExtraFieldInWriteRecord() throws IOException {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (final WriteJsonResult writer = new WriteJsonResult(Mockito.mock(ComponentLog.class), schema, new SchemaNameAsAttribute(), baos, false, NullSuppression.NEVER_SUPPRESS, null, null, null)) {
        writer.beginRecordSet();
        writer.writeRecord(record);
        writer.finishRecordSet();
    }
    final byte[] data = baos.toByteArray();
    final String expected = "[{\"id\":\"1\",\"name\":null}]";
    final String output = new String(data, StandardCharsets.UTF_8);
    assertEquals(expected, output);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) SchemaNameAsAttribute(org.apache.nifi.schema.access.SchemaNameAsAttribute) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ComponentLog(org.apache.nifi.logging.ComponentLog) LinkedHashMap(java.util.LinkedHashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 54 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestLookupRecord method testAddFieldsToExistingRecord.

@Test
public void testAddFieldsToExistingRecord() throws InitializationException, IOException {
    final RecordLookup lookupService = new RecordLookup();
    runner.addControllerService("lookup", lookupService);
    runner.enableControllerService(lookupService);
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("favorite", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("least", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Record sports = new MapRecord(schema, new HashMap<String, Object>());
    sports.setValue("favorite", "basketball");
    sports.setValue("least", "soccer");
    lookupService.addValue("John Doe", sports);
    recordReader = new MockRecordParser();
    recordReader.addSchemaField("name", RecordFieldType.STRING);
    recordReader.addSchemaField("age", RecordFieldType.INT);
    recordReader.addSchemaField("favorite", RecordFieldType.STRING);
    recordReader.addSchemaField("least", RecordFieldType.STRING);
    recordReader.addRecord("John Doe", 48, null, "baseball");
    runner.addControllerService("reader", recordReader);
    runner.enableControllerService(recordReader);
    runner.setProperty("lookup", "/name");
    runner.setProperty(LookupRecord.RESULT_RECORD_PATH, "/");
    runner.setProperty(LookupRecord.RESULT_CONTENTS, LookupRecord.RESULT_RECORD_FIELDS);
    runner.enqueue("");
    runner.run();
    final MockFlowFile out = runner.getFlowFilesForRelationship(LookupRecord.REL_MATCHED).get(0);
    out.assertContentEquals("John Doe,48,basketball,soccer\n");
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) MockFlowFile(org.apache.nifi.util.MockFlowFile) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Example 55 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestLookupRecord method testAddFieldsToNonExistentRecord.

/**
 * If the output fields are added to a record that doesn't exist, the result should be that a Record is
 * created and the results added to it.
 */
@Test
public void testAddFieldsToNonExistentRecord() throws InitializationException {
    final RecordLookup lookupService = new RecordLookup();
    runner.addControllerService("lookup", lookupService);
    runner.enableControllerService(lookupService);
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("favorite", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("least", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Record sports = new MapRecord(schema, new HashMap<String, Object>());
    sports.setValue("favorite", "basketball");
    sports.setValue("least", "soccer");
    lookupService.addValue("John Doe", sports);
    recordReader = new MockRecordParser();
    recordReader.addSchemaField("name", RecordFieldType.STRING);
    recordReader.addSchemaField("age", RecordFieldType.INT);
    recordReader.addSchemaField("sport", RecordFieldType.RECORD);
    recordReader.addRecord("John Doe", 48, null);
    runner.addControllerService("reader", recordReader);
    runner.enableControllerService(recordReader);
    runner.setProperty("lookup", "/name");
    runner.setProperty(LookupRecord.RESULT_RECORD_PATH, "/sport");
    runner.setProperty(LookupRecord.RESULT_CONTENTS, LookupRecord.RESULT_RECORD_FIELDS);
    runner.enqueue("");
    runner.run();
    final MockFlowFile out = runner.getFlowFilesForRelationship(LookupRecord.REL_MATCHED).get(0);
    // We can't be sure of the order of the fields in the record, so we allow either 'least' or 'favorite' to be first
    final String outputContents = new String(out.toByteArray());
    assertTrue(outputContents.equals("John Doe,48,MapRecord[{favorite=basketball, least=soccer}]\n") || outputContents.equals("John Doe,48,MapRecord[{least=soccer, favorite=basketball}]\n"));
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) MockFlowFile(org.apache.nifi.util.MockFlowFile) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Aggregations

SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)171 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)152 Test (org.junit.Test)146 RecordField (org.apache.nifi.serialization.record.RecordField)127 Record (org.apache.nifi.serialization.record.Record)120 ArrayList (java.util.ArrayList)114 MapRecord (org.apache.nifi.serialization.record.MapRecord)86 HashMap (java.util.HashMap)82 InputStream (java.io.InputStream)58 FileInputStream (java.io.FileInputStream)53 ByteArrayInputStream (java.io.ByteArrayInputStream)52 ComponentLog (org.apache.nifi.logging.ComponentLog)50 LinkedHashMap (java.util.LinkedHashMap)33 File (java.io.File)30 DataType (org.apache.nifi.serialization.record.DataType)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)19 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)19 List (java.util.List)18 IOException (java.io.IOException)17