@Override public void updateValue(final Object newValue) { final Optional<Record> parentRecord = getParentRecord(); if (!parentRecord.isPresent()) { if (value instanceof Record) { ((Record) value).setValue(getField().getFieldName(), newValue); return; } else if (value == null) { return; // value is null, nothing to update } else { throw new UnsupportedOperationException("Cannot update the field value because the value is not associated with any record"); } } parentRecord.get().setValue(getField().getFieldName(), newValue); } }
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(); } } }
private Record updateRecord(final List<FieldValue> destinationFields, final List<FieldValue> selectedFields, final Record record) { if (destinationFields.size() == 1 && !destinationFields.get(0).getParentRecord().isPresent()) { final Object replacement = getReplacementObject(selectedFields); if (replacement == null) { return record; } if (replacement instanceof Record) { return (Record) replacement; } final List<RecordField> fields = selectedFields.stream().map(FieldValue::getField).collect(Collectors.toList()); final RecordSchema schema = new SimpleRecordSchema(fields); final Record mapRecord = new MapRecord(schema, new HashMap<>()); for (final FieldValue selectedField : selectedFields) { mapRecord.setValue(selectedField.getField().getFieldName(), selectedField.getValue()); } return mapRecord; } else { for (final FieldValue fieldVal : destinationFields) { fieldVal.updateValue(getReplacementObject(selectedFields)); } return record; } }
private void recursivelyAddParentFields(Record recordToWrite, FieldValue fieldValue) { try { // we get the parent data FieldValue parentField = fieldValue.getParent().get(); Record parentRecord = fieldValue.getParentRecord().get(); // for each field of the parent for (String field : parentRecord.getSchema().getFieldNames()) { // if and only if there is not an already existing field with this name // (we want to give priority to the deeper existing fields) if(recordToWrite.getValue(field) == null) { // Updates the value of the field with the given name to the given value. // If the field specified is not present in the schema, will do nothing. recordToWrite.setValue(field, parentRecord.getValue(field)); } } // recursive call recursivelyAddParentFields(recordToWrite, parentField); } catch (NoSuchElementException e) { return; } } });
@Test public void testAddFieldsToExistingRecord() throws InitializationException, IOException { final RecordLookup lookupService = new RecordLookup(); runner.addControllerService("lookup", lookupService); runner.enableControllerService(lookupService); final List<RecordField> fields = new ArrayList<>(); fields.add(new RecordField("favorite", RecordFieldType.STRING.getDataType())); fields.add(new RecordField("least", RecordFieldType.STRING.getDataType())); final RecordSchema schema = new SimpleRecordSchema(fields); final Record sports = new MapRecord(schema, new HashMap<String, Object>()); sports.setValue("favorite", "basketball"); sports.setValue("least", "soccer"); lookupService.addValue("John Doe", sports); recordReader = new MockRecordParser(); recordReader.addSchemaField("name", RecordFieldType.STRING); recordReader.addSchemaField("age", RecordFieldType.INT); recordReader.addSchemaField("favorite", RecordFieldType.STRING); recordReader.addSchemaField("least", RecordFieldType.STRING); recordReader.addRecord("John Doe", 48, null, "baseball"); runner.addControllerService("reader", recordReader); runner.enableControllerService(recordReader); runner.setProperty("lookup", "/name"); runner.setProperty(LookupRecord.RESULT_RECORD_PATH, "/"); runner.setProperty(LookupRecord.RESULT_CONTENTS, LookupRecord.RESULT_RECORD_FIELDS); runner.enqueue(""); runner.run(); final MockFlowFile out = runner.getFlowFilesForRelationship(LookupRecord.REL_MATCHED).get(0); out.assertContentEquals("John Doe,48,basketball,soccer\n"); }
@Override public void updateValue(final Object newValue) { final Optional<Record> parentRecord = getParentRecord(); if (!parentRecord.isPresent()) { if (value instanceof Record) { ((Record) value).setValue(getField().getFieldName(), newValue); return; } else if (value == null) { return; // value is null, nothing to update } else { throw new UnsupportedOperationException("Cannot update the field value because the value is not associated with any record"); } } parentRecord.get().setValue(getField().getFieldName(), newValue); } }
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(); } } }
private Record updateRecord(final List<FieldValue> destinationFields, final List<FieldValue> selectedFields, final Record record) { if (destinationFields.size() == 1 && !destinationFields.get(0).getParentRecord().isPresent()) { final Object replacement = getReplacementObject(selectedFields); if (replacement == null) { return record; } if (replacement instanceof Record) { return (Record) replacement; } final List<RecordField> fields = selectedFields.stream().map(FieldValue::getField).collect(Collectors.toList()); final RecordSchema schema = new SimpleRecordSchema(fields); final Record mapRecord = new MapRecord(schema, new HashMap<>()); for (final FieldValue selectedField : selectedFields) { mapRecord.setValue(selectedField.getField().getFieldName(), selectedField.getValue()); } return mapRecord; } else { for (final FieldValue fieldVal : destinationFields) { fieldVal.updateValue(getReplacementObject(selectedFields)); } return record; } }
private void recursivelyAddParentFields(Record recordToWrite, FieldValue fieldValue) { try { // we get the parent data FieldValue parentField = fieldValue.getParent().get(); Record parentRecord = fieldValue.getParentRecord().get(); // for each field of the parent for (String field : parentRecord.getSchema().getFieldNames()) { // if and only if there is not an already existing field with this name // (we want to give priority to the deeper existing fields) if(recordToWrite.getValue(field) == null) { // Updates the value of the field with the given name to the given value. // If the field specified is not present in the schema, will do nothing. recordToWrite.setValue(field, parentRecord.getValue(field)); } } // recursive call recursivelyAddParentFields(recordToWrite, parentField); } catch (NoSuchElementException e) { return; } } });