Search in sources :

Example 91 with RecordSchema

use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.

the class TestRecordPath method testSingleArrayIndex.

@Test
public void testSingleArrayIndex() {
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("numbers", new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    final Record record = new MapRecord(schema, values);
    final FieldValue fieldValue = RecordPath.compile("/numbers[3]").evaluate(record).getSelectedFields().findFirst().get();
    assertEquals("numbers", fieldValue.getField().getFieldName());
    assertEquals(3, fieldValue.getValue());
    assertEquals(record, fieldValue.getParentRecord().get());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) HashMap(java.util.HashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 92 with RecordSchema

use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.

the class TestRecordPath method testMultiArrayIndex.

@Test
public void testMultiArrayIndex() {
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("numbers", new Object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    final Record record = new MapRecord(schema, values);
    final List<FieldValue> fieldValues = RecordPath.compile("/numbers[3,6, -1, -2]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    int i = 0;
    final int[] expectedValues = new int[] { 3, 6, 9, 8 };
    for (final FieldValue fieldValue : fieldValues) {
        assertEquals("numbers", fieldValue.getField().getFieldName());
        assertEquals(expectedValues[i++], fieldValue.getValue());
        assertEquals(record, fieldValue.getParentRecord().get());
    }
    RecordPath.compile("/numbers[3,6, -1, -2]").evaluate(record).getSelectedFields().forEach(field -> field.updateValue(99));
    assertArrayEquals(new Object[] { 0, 1, 2, 99, 4, 5, 99, 7, 99, 99 }, (Object[]) values.get("numbers"));
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) HashMap(java.util.HashMap) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 93 with RecordSchema

use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.

the class TestRecordPath method getAccountSchema.

private RecordSchema getAccountSchema() {
    final List<RecordField> accountFields = new ArrayList<>();
    accountFields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    accountFields.add(new RecordField("balance", RecordFieldType.DOUBLE.getDataType()));
    final RecordSchema accountSchema = new SimpleRecordSchema(accountFields);
    return accountSchema;
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema)

Example 94 with RecordSchema

use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.

the class TestRecordPath method testSubstringBeforeFunction.

@Test
public void testSubstringBeforeFunction() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name", "John Doe");
    final Record record = new MapRecord(schema, values);
    assertEquals("John", RecordPath.compile("substringBefore(/name, ' ')").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John Doe", RecordPath.compile("substringBefore(/name, 'XYZ')").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John Doe", RecordPath.compile("substringBefore(/name, '')").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John D", RecordPath.compile("substringBeforeLast(/name, 'o')").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John Doe", RecordPath.compile("substringBeforeLast(/name, 'XYZ')").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John Doe", RecordPath.compile("substringBeforeLast(/name, '')").evaluate(record).getSelectedFields().findFirst().get().getValue());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 95 with RecordSchema

use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.

the class TestRecordPath method testContainsRegex.

@Test
public void testContainsRegex() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name", "John Doe");
    final Record record = new MapRecord(schema, values);
    assertEquals("John Doe", RecordPath.compile("/name[containsRegex(., 'o')]").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John Doe", RecordPath.compile("/name[containsRegex(., '[xo]')]").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals(0L, RecordPath.compile("/name[containsRegex(., 'x')]").evaluate(record).getSelectedFields().count());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Aggregations

RecordSchema (org.apache.nifi.serialization.record.RecordSchema)243 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)178 Test (org.junit.Test)168 Record (org.apache.nifi.serialization.record.Record)147 RecordField (org.apache.nifi.serialization.record.RecordField)138 ArrayList (java.util.ArrayList)107 MapRecord (org.apache.nifi.serialization.record.MapRecord)94 HashMap (java.util.HashMap)88 InputStream (java.io.InputStream)79 ByteArrayInputStream (java.io.ByteArrayInputStream)64 FileInputStream (java.io.FileInputStream)56 ComponentLog (org.apache.nifi.logging.ComponentLog)54 IOException (java.io.IOException)44 LinkedHashMap (java.util.LinkedHashMap)36 DataType (org.apache.nifi.serialization.record.DataType)36 File (java.io.File)31 Schema (org.apache.avro.Schema)29 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)29 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26