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"));
}
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());
}
}
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());
}
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());
}
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());
}
Aggregations