Search in sources :

Example 71 with MapRecord

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

the class TestRecordPath method testUpdateMap.

@Test
@SuppressWarnings("unchecked")
public void testUpdateMap() {
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("city", "New York");
    attributes.put("state", "NY");
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name", "John Doe");
    values.put("attributes", attributes);
    final Record record = new MapRecord(schema, values);
    RecordPath.compile("/attributes['city']").evaluate(record).getSelectedFields().findFirst().get().updateValue("Boston");
    assertEquals("Boston", ((Map<String, Object>) record.getValue("attributes")).get("city"));
}
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 72 with MapRecord

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

the class TestRecordPath method testSingleArrayRange.

@Test
public void testSingleArrayRange() {
    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[0..1]").evaluate(record).getSelectedFields().collect(Collectors.toList());
    for (final FieldValue fieldValue : fieldValues) {
        assertEquals("numbers", fieldValue.getField().getFieldName());
        assertEquals(record, fieldValue.getParentRecord().get());
    }
    assertEquals(2, fieldValues.size());
    for (int i = 0; i < 1; i++) {
        assertEquals(i, fieldValues.get(0).getValue());
    }
}
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 73 with MapRecord

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

the class TestRecordPath method testSubstringFunction.

@Test
public void testSubstringFunction() {
    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);
    final FieldValue fieldValue = RecordPath.compile("substring(/name, 0, 4)").evaluate(record).getSelectedFields().findFirst().get();
    assertEquals("John", fieldValue.getValue());
    assertEquals("John", RecordPath.compile("substring(/name, 0, -5)").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("", RecordPath.compile("substring(/name, 1000, 1005)").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("", RecordPath.compile("substring(/name, 4, 3)").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("John Doe", RecordPath.compile("substring(/name, 0, 10000)").evaluate(record).getSelectedFields().findFirst().get().getValue());
    assertEquals("", RecordPath.compile("substring(/name, -50, -1)").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 74 with MapRecord

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

the class TestRecordPath method testPredicateDoesNotIncludeFieldsThatDontHaveRelativePath.

@Test
public void testPredicateDoesNotIncludeFieldsThatDontHaveRelativePath() {
    final List<RecordField> addressFields = new ArrayList<>();
    addressFields.add(new RecordField("city", RecordFieldType.STRING.getDataType()));
    addressFields.add(new RecordField("state", RecordFieldType.STRING.getDataType()));
    addressFields.add(new RecordField("zip", RecordFieldType.STRING.getDataType()));
    final RecordSchema addressSchema = new SimpleRecordSchema(addressFields);
    final List<RecordField> detailsFields = new ArrayList<>();
    detailsFields.add(new RecordField("position", RecordFieldType.STRING.getDataType()));
    detailsFields.add(new RecordField("managerName", RecordFieldType.STRING.getDataType()));
    final RecordSchema detailsSchema = new SimpleRecordSchema(detailsFields);
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("address", RecordFieldType.RECORD.getRecordDataType(addressSchema)));
    fields.add(new RecordField("details", RecordFieldType.RECORD.getRecordDataType(detailsSchema)));
    final RecordSchema recordSchema = new SimpleRecordSchema(fields);
    final Record record = new MapRecord(recordSchema, new HashMap<>());
    record.setValue("name", "John Doe");
    final Record addressRecord = new MapRecord(addressSchema, new HashMap<>());
    addressRecord.setValue("city", "San Francisco");
    addressRecord.setValue("state", "CA");
    addressRecord.setValue("zip", "12345");
    record.setValue("address", addressRecord);
    final Record detailsRecord = new MapRecord(detailsSchema, new HashMap<>());
    detailsRecord.setValue("position", "Developer");
    detailsRecord.setValue("managerName", "Jane Doe");
    record.setValue("details", detailsRecord);
    final RecordPath recordPath = RecordPath.compile("/*[./state != 'NY']");
    final RecordPathResult result = recordPath.evaluate(record);
    final List<FieldValue> fieldValues = result.getSelectedFields().collect(Collectors.toList());
    assertEquals(1, fieldValues.size());
    final FieldValue fieldValue = fieldValues.get(0);
    assertEquals("address", fieldValue.getField().getFieldName());
    assertEquals("12345", RecordPath.compile("/*[./state != 'NY']/zip").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) 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 75 with MapRecord

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

the class TestRecordPath method testCompareWithEmbeddedPaths.

@Test
public void testCompareWithEmbeddedPaths() {
    final Map<String, Object> accountValues1 = new HashMap<>();
    accountValues1.put("id", 1);
    accountValues1.put("balance", 10_000.00D);
    final Record accountRecord1 = new MapRecord(getAccountSchema(), accountValues1);
    final Map<String, Object> accountValues2 = new HashMap<>();
    accountValues2.put("id", 2);
    accountValues2.put("balance", 48.02D);
    final Record accountRecord2 = new MapRecord(getAccountSchema(), accountValues2);
    final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
    final Map<String, Object> values = new HashMap<>();
    values.put("id", 48);
    values.put("name", "John Doe");
    values.put("accounts", new Object[] { accountRecord1, accountRecord2 });
    final Record record = new MapRecord(schema, values);
    final RecordPath recordPath = RecordPath.compile("/accounts[0..-1][./balance > 100]");
    List<FieldValue> fieldValues = recordPath.evaluate(record).getSelectedFields().collect(Collectors.toList());
    assertEquals(1, fieldValues.size());
    final FieldValue fieldValue = fieldValues.get(0);
    assertEquals("accounts", fieldValue.getField().getFieldName());
    assertEquals(0, ((ArrayIndexFieldValue) fieldValue).getArrayIndex());
    assertEquals(record, fieldValue.getParentRecord().get());
    assertEquals(accountRecord1, fieldValue.getValue());
}
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)

Aggregations

MapRecord (org.apache.nifi.serialization.record.MapRecord)101 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)89 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)88 Record (org.apache.nifi.serialization.record.Record)82 Test (org.junit.Test)79 HashMap (java.util.HashMap)73 RecordField (org.apache.nifi.serialization.record.RecordField)71 ArrayList (java.util.ArrayList)59 LinkedHashMap (java.util.LinkedHashMap)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)17 DataType (org.apache.nifi.serialization.record.DataType)12 ComponentLog (org.apache.nifi.logging.ComponentLog)11 Date (java.sql.Date)7 CSVFormat (org.apache.commons.csv.CSVFormat)7 SchemaValidationResult (org.apache.nifi.serialization.record.validation.SchemaValidationResult)7 Time (java.sql.Time)6 Timestamp (java.sql.Timestamp)6 DateFormat (java.text.DateFormat)6 Map (java.util.Map)6