use of org.apache.nifi.serialization.record.DataType in project nifi by apache.
the class DataTypeUtils method isScalarValue.
public static boolean isScalarValue(final DataType dataType, final Object value) {
final RecordFieldType fieldType = dataType.getFieldType();
final RecordFieldType chosenType;
if (fieldType == RecordFieldType.CHOICE) {
final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
final DataType chosenDataType = chooseDataType(value, choiceDataType);
if (chosenDataType == null) {
return false;
}
chosenType = chosenDataType.getFieldType();
} else {
chosenType = fieldType;
}
switch(chosenType) {
case ARRAY:
case MAP:
case RECORD:
return false;
}
return true;
}
use of org.apache.nifi.serialization.record.DataType in project nifi by apache.
the class DataTypeUtils method merge.
public static RecordField merge(final RecordField thisField, final RecordField otherField) {
final String fieldName = thisField.getFieldName();
final Set<String> aliases = new HashSet<>();
aliases.addAll(thisField.getAliases());
aliases.addAll(otherField.getAliases());
final Object defaultValue;
if (thisField.getDefaultValue() == null && otherField.getDefaultValue() != null) {
defaultValue = otherField.getDefaultValue();
} else {
defaultValue = thisField.getDefaultValue();
}
final DataType dataType;
if (thisField.getDataType().equals(otherField.getDataType())) {
dataType = thisField.getDataType();
} else {
dataType = RecordFieldType.CHOICE.getChoiceDataType(thisField.getDataType(), otherField.getDataType());
}
return new RecordField(fieldName, dataType, defaultValue, aliases, thisField.isNullable() || otherField.isNullable());
}
use of org.apache.nifi.serialization.record.DataType in project nifi by apache.
the class DataTypeUtils method convertRecordFieldtoObject.
/**
* Creates a native Java object from a given object of a specified type. Non-scalar (complex, nested, etc.) data types are processed iteratively/recursively, such that all
* included objects are native Java objects, rather than Record API objects or implementation-specific objects.
* @param value The object to be converted
* @param dataType The type of the provided object
* @return An object representing a native Java conversion of the given input object
*/
public static Object convertRecordFieldtoObject(final Object value, final DataType dataType) {
if (value == null) {
return null;
}
if (value instanceof Record) {
Record record = (Record) value;
RecordSchema recordSchema = record.getSchema();
if (recordSchema == null) {
throw new IllegalTypeConversionException("Cannot convert value of type Record to Map because Record does not have an associated Schema");
}
final Map<String, Object> recordMap = new HashMap<>();
for (RecordField field : recordSchema.getFields()) {
final DataType fieldDataType = field.getDataType();
final String fieldName = field.getFieldName();
Object fieldValue = record.getValue(fieldName);
if (fieldValue == null) {
recordMap.put(fieldName, null);
} else if (isScalarValue(fieldDataType, fieldValue)) {
recordMap.put(fieldName, fieldValue);
} else if (fieldDataType instanceof RecordDataType) {
Record nestedRecord = (Record) fieldValue;
recordMap.put(fieldName, convertRecordFieldtoObject(nestedRecord, fieldDataType));
} else if (fieldDataType instanceof MapDataType) {
recordMap.put(fieldName, convertRecordMapToJavaMap((Map) fieldValue, ((MapDataType) fieldDataType).getValueType()));
} else if (fieldDataType instanceof ArrayDataType) {
recordMap.put(fieldName, convertRecordArrayToJavaArray((Object[]) fieldValue, ((ArrayDataType) fieldDataType).getElementType()));
} else {
throw new IllegalTypeConversionException("Cannot convert value [" + fieldValue + "] of type " + fieldDataType.toString() + " to Map for field " + fieldName + " because the type is not supported");
}
}
return recordMap;
} else if (value instanceof Map) {
return convertRecordMapToJavaMap((Map) value, ((MapDataType) dataType).getValueType());
} else if (dataType != null && isScalarValue(dataType, value)) {
return value;
}
throw new IllegalTypeConversionException("Cannot convert value of class " + value.getClass().getName() + " because the type is not supported");
}
use of org.apache.nifi.serialization.record.DataType in project nifi by apache.
the class ArrayIndexPath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)).filter(fieldValue -> fieldValue.getValue() != null && ((Object[]) fieldValue.getValue()).length > getArrayIndex(((Object[]) fieldValue.getValue()).length)).map(fieldValue -> {
final ArrayDataType arrayDataType = (ArrayDataType) fieldValue.getField().getDataType();
final DataType elementDataType = arrayDataType.getElementType();
final RecordField arrayField = new RecordField(fieldValue.getField().getFieldName(), elementDataType);
final Object[] values = (Object[]) fieldValue.getValue();
final int arrayIndex = getArrayIndex(values.length);
final RecordField elementField = new RecordField(arrayField.getFieldName(), elementDataType);
final FieldValue result = new ArrayIndexFieldValue(values[arrayIndex], elementField, fieldValue, arrayIndex);
return result;
});
}
use of org.apache.nifi.serialization.record.DataType in project nifi by apache.
the class SingularMapKeyPath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.MAP)).map(fieldValue -> {
final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
return new MapEntryFieldValue(getMapValue(fieldValue), elementField, fieldValue, mapKey);
});
}
Aggregations