use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestRecordPath method testRelativePathOnly.
@Test
public void testRelativePathOnly() {
final RecordSchema schema = new SimpleRecordSchema(getDefaultFields());
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 recordFieldValue = new StandardFieldValue(record, new RecordField("record", RecordFieldType.RECORD.getDataType()), null);
final List<FieldValue> fieldValues = RecordPath.compile("./name").evaluate(record, recordFieldValue).getSelectedFields().collect(Collectors.toList());
assertEquals(1, fieldValues.size());
final FieldValue fieldValue = fieldValues.get(0);
assertEquals("John Doe", fieldValue.getValue());
assertEquals(record, fieldValue.getParentRecord().get());
assertEquals("name", fieldValue.getField().getFieldName());
}
use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestRecordPath method testMapWildcard.
@Test
public void testMapWildcard() {
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);
final List<FieldValue> fieldValues = RecordPath.compile("/attributes[*]").evaluate(record).getSelectedFields().collect(Collectors.toList());
assertEquals(2, fieldValues.size());
assertEquals("New York", fieldValues.get(0).getValue());
assertEquals("NY", fieldValues.get(1).getValue());
for (final FieldValue fieldValue : fieldValues) {
assertEquals("attributes", fieldValue.getField().getFieldName());
assertEquals(record, fieldValue.getParentRecord().get());
}
RecordPath.compile("/attributes[*]").evaluate(record).getSelectedFields().forEach(field -> field.updateValue("Unknown"));
assertEquals("Unknown", attributes.get("city"));
assertEquals("Unknown", attributes.get("state"));
RecordPath.compile("/attributes[*][fieldName(.) = 'attributes']").evaluate(record).getSelectedFields().forEach(field -> field.updateValue("Unknown"));
assertEquals("Unknown", attributes.get("city"));
assertEquals("Unknown", attributes.get("state"));
}
use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestRecordPath method testReplace.
@Test
public void testReplace() {
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[replace(../id, 48, 18) = 18]").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals(0L, RecordPath.compile("/name[replace(../id, 48, 18) = 48]").evaluate(record).getSelectedFields().count());
assertEquals("Jane Doe", RecordPath.compile("replace(/name, 'ohn', 'ane')").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("John Doe", RecordPath.compile("replace(/name, 'ohnny', 'ane')").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("John 48", RecordPath.compile("replace(/name, 'Doe', /id)").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("23", RecordPath.compile("replace(/id, 48, 23)").evaluate(record).getSelectedFields().findFirst().get().getValue());
}
use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestRecordPath method testFormatDateFromString.
@Test
public void testFormatDateFromString() throws ParseException {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
fields.add(new RecordField("date", RecordFieldType.DATE.getDataType()));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> values = new HashMap<>();
values.put("id", 48);
values.put("date", "2017-10-20T11:00:00Z");
final Record record = new MapRecord(schema, values);
final FieldValue fieldValue = RecordPath.compile("format( toDate(/date, \"yyyy-MM-dd'T'HH:mm:ss'Z'\"), 'yyyy-MM-dd' )").evaluate(record).getSelectedFields().findFirst().get();
assertEquals("2017-10-20", fieldValue.getValue());
final FieldValue fieldValueUnchanged = RecordPath.compile("format( toDate(/date, \"yyyy-MM-dd'T'HH:mm:ss'Z'\"), 'INVALID' )").evaluate(record).getSelectedFields().findFirst().get();
assertEquals(DataTypeUtils.getDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse("2017-10-20T11:00:00Z"), fieldValueUnchanged.getValue());
}
use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestRecordPath method testPredicateInMiddleOfPath.
@Test
public void testPredicateInMiddleOfPath() {
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]/id");
List<FieldValue> fieldValues = recordPath.evaluate(record).getSelectedFields().collect(Collectors.toList());
assertEquals(1, fieldValues.size());
final FieldValue fieldValue = fieldValues.get(0);
assertEquals("id", fieldValue.getField().getFieldName());
assertEquals(accountRecord1, fieldValue.getParentRecord().get());
assertEquals(1, fieldValue.getValue());
}
Aggregations