org.apache.nifi.serialization.record.Record.setValue()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(142)

本文整理了Java中org.apache.nifi.serialization.record.Record.setValue方法的一些代码示例,展示了Record.setValue的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Record.setValue方法的具体详情如下:
包路径:org.apache.nifi.serialization.record.Record
类名称:Record
方法名:setValue

Record.setValue介绍

[英]Updates the value of the field with the given name to the given value. If the field specified is not present in this Record's schema, this method will do nothing. If this method does change any value in the Record, any SerializedForm that was provided will be removed (i.e., any subsequent call to #getSerializedForm() will return an empty Optional).
[中]将具有给定名称的字段的值更新为给定值。如果指定的字段不在此记录的架构中,则此方法将不起任何作用。如果此方法确实更改了记录中的任何值,则提供的任何SerializedForm都将被删除(即,对#getSerializedForm()的任何后续调用都将返回空的可选值)。

代码示例

代码示例来源:origin: apache/nifi

@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);
  }
}

代码示例来源:origin: apache/nifi

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

代码示例来源:origin: apache/nifi

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;
  }
}

代码示例来源:origin: apache/nifi

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;
    }
  }
});

代码示例来源:origin: apache/nifi

destinationRecord.setValue(fieldName, value);
parentOption.get().setValue(fieldVal.getField().getFieldName(), lookupRecord);

代码示例来源:origin: apache/nifi

final Record sports = new MapRecord(schema, new HashMap<String, Object>());
sports.setValue("favorite", "basketball");
sports.setValue("least", "soccer");

代码示例来源:origin: apache/nifi

final Record sports = new MapRecord(schema, new HashMap<String, Object>());
sports.setValue("favorite", "basketball");
sports.setValue("least", "soccer");

代码示例来源:origin: apache/nifi

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

代码示例来源:origin: org.apache.nifi/nifi-record-path

@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);
  }
}

代码示例来源:origin: org.apache.nifi/nifi-standard-processors

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

代码示例来源:origin: org.apache.nifi/nifi-standard-processors

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;
  }
}

代码示例来源:origin: org.apache.nifi/nifi-standard-processors

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;
    }
  }
});

代码示例来源:origin: org.apache.nifi/nifi-standard-processors

destinationRecord.setValue(fieldName, value);
parentOption.get().setValue(fieldVal.getField().getFieldName(), lookupRecord);

相关文章