use of org.apache.nifi.serialization.record.MapRecord in project nifi by apache.
the class TestRecordPath method testWildcardWithArray.
@Test
public void testWildcardWithArray() {
final Map<String, Object> accountValues = new HashMap<>();
accountValues.put("id", 1);
accountValues.put("balance", 123.45D);
final Record accountRecord = new MapRecord(getAccountSchema(), accountValues);
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[] { accountRecord });
final Record record = new MapRecord(schema, values);
final List<FieldValue> fieldValues = RecordPath.compile("/*[0]").evaluate(record).getSelectedFields().collect(Collectors.toList());
assertEquals(1, fieldValues.size());
final FieldValue fieldValue = fieldValues.get(0);
assertEquals("accounts", fieldValue.getField().getFieldName());
assertEquals(record, fieldValue.getParentRecord().get());
assertEquals(accountRecord, fieldValue.getValue());
final Map<String, Object> updatedAccountValues = new HashMap<>(accountValues);
updatedAccountValues.put("balance", 122.44D);
final Record updatedAccountRecord = new MapRecord(getAccountSchema(), updatedAccountValues);
RecordPath.compile("/*[0]").evaluate(record).getSelectedFields().forEach(field -> field.updateValue(updatedAccountRecord));
final Object[] accountRecords = (Object[]) record.getValue("accounts");
assertEquals(1, accountRecords.length);
final Record recordToVerify = (Record) accountRecords[0];
assertEquals(122.44D, recordToVerify.getValue("balance"));
assertEquals(48, record.getValue("id"));
assertEquals("John Doe", record.getValue("name"));
}
use of org.apache.nifi.serialization.record.MapRecord in project nifi by apache.
the class TestRecordPath method testFieldName.
@Test
public void testFieldName() {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
final RecordSchema schema = new SimpleRecordSchema(fields);
final Map<String, Object> values = new HashMap<>();
values.put("name", "John Doe");
final Record record = new MapRecord(schema, values);
assertEquals("name", RecordPath.compile("fieldName(/name)").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("name", RecordPath.compile("fieldName(/*)").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("John Doe", RecordPath.compile("//*[startsWith(fieldName(.), 'na')]").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("name", RecordPath.compile("fieldName(//*[startsWith(fieldName(.), 'na')])").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals("John Doe", RecordPath.compile("//name[not(startsWith(fieldName(.), 'xyz'))]").evaluate(record).getSelectedFields().findFirst().get().getValue());
assertEquals(0L, RecordPath.compile("//name[not(startsWith(fieldName(.), 'n'))]").evaluate(record).getSelectedFields().count());
}
use of org.apache.nifi.serialization.record.MapRecord in project nifi by apache.
the class TestRecordPath method testRelativePathAgainstNonRecordField.
@Test
public void testRelativePathAgainstNonRecordField() {
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("root", RecordFieldType.RECORD.getRecordDataType(record.getSchema())), null);
final FieldValue nameFieldValue = new StandardFieldValue("John Doe", new RecordField("name", RecordFieldType.STRING.getDataType()), recordFieldValue);
final List<FieldValue> fieldValues = RecordPath.compile(".").evaluate(record, nameFieldValue).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());
fieldValue.updateValue("Jane Doe");
assertEquals("Jane Doe", record.getValue("name"));
}
use of org.apache.nifi.serialization.record.MapRecord in project nifi by apache.
the class PutMongoRecordIT method testInsertNestedRecords.
@Test
public void testInsertNestedRecords() throws Exception {
TestRunner runner = init();
recordReader.addSchemaField("id", RecordFieldType.INT);
final List<RecordField> personFields = new ArrayList<>();
final RecordField nameField = new RecordField("name", RecordFieldType.STRING.getDataType());
final RecordField ageField = new RecordField("age", RecordFieldType.INT.getDataType());
final RecordField sportField = new RecordField("sport", RecordFieldType.STRING.getDataType());
personFields.add(nameField);
personFields.add(ageField);
personFields.add(sportField);
final RecordSchema personSchema = new SimpleRecordSchema(personFields);
recordReader.addSchemaField("person", RecordFieldType.RECORD);
recordReader.addRecord(1, new MapRecord(personSchema, new HashMap<String, Object>() {
{
put("name", "John Doe");
put("age", 48);
put("sport", "Soccer");
}
}));
recordReader.addRecord(2, new MapRecord(personSchema, new HashMap<String, Object>() {
{
put("name", "Jane Doe");
put("age", 47);
put("sport", "Tennis");
}
}));
recordReader.addRecord(3, new MapRecord(personSchema, new HashMap<String, Object>() {
{
put("name", "Sally Doe");
put("age", 47);
put("sport", "Curling");
}
}));
recordReader.addRecord(4, new MapRecord(personSchema, new HashMap<String, Object>() {
{
put("name", "Jimmy Doe");
put("age", 14);
put("sport", null);
}
}));
runner.enqueue("");
runner.run();
runner.assertAllFlowFilesTransferred(PutMongoRecord.REL_SUCCESS, 1);
MockFlowFile out = runner.getFlowFilesForRelationship(PutMongoRecord.REL_SUCCESS).get(0);
// verify 1 doc inserted into the collection
assertEquals(4, collection.count());
// assertEquals(doc, collection.find().first());
}
use of org.apache.nifi.serialization.record.MapRecord in project nifi by apache.
the class UpdateRecord method getReplacementObject.
private Object getReplacementObject(final List<FieldValue> selectedFields) {
if (selectedFields.size() > 1) {
final List<RecordField> fields = selectedFields.stream().map(FieldValue::getField).collect(Collectors.toList());
final RecordSchema schema = new SimpleRecordSchema(fields);
final Record record = new MapRecord(schema, new HashMap<>());
for (final FieldValue fieldVal : selectedFields) {
record.setValue(fieldVal.getField().getFieldName(), fieldVal.getValue());
}
return record;
}
if (selectedFields.isEmpty()) {
return null;
} else {
return selectedFields.get(0).getValue();
}
}
Aggregations