Search in sources :

Example 81 with MapRecord

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"));
}
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 82 with MapRecord

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());
}
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 83 with MapRecord

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"));
}
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) 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 84 with MapRecord

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());
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MockFlowFile(org.apache.nifi.util.MockFlowFile) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) ArrayList(java.util.ArrayList) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 85 with MapRecord

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();
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) FieldValue(org.apache.nifi.record.path.FieldValue) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

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