Support Questions
Find answers, ask questions, and share your expertise

Apache NiFi custom processor expression language

New Contributor

I'm trying to make a custom processor in Apache NiFi that can add an attribute/string to the JSON object in the flowfile content. At the moment it works when I just use a string but it's not working when I use NiFi's expression language although I have it supported in my code.

The expression language is 100% correct as it works in another processor and I've also tried different attributes to make sure it's not the attribute. The property:

public static final PropertyDescriptor ADD_ATTRIBUTE = new PropertyDescriptor
        .Builder().name("Add Attribute")
        .description("Example Property")
        .required(true)
        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
        .expressionLanguageSupported(true)
        .build();

Later in my code when I want to get the value and put in the JSON object I use:

jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions().getValue());

I also made a Unit Test and it works when I assign a text value to the testrunner.setProperty. However I don't know how I can assign an attribute to the testrunner or how I can use expression language in my test. Thanks in advance for any suggestions or a solution!

1 ACCEPTED SOLUTION

Super Guru

If the expression refers to attributes on a flow file, you will need to pass a reference to the flow file into evaluateAttributeExpressions:

FlowFile flowFile = session.get();
jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue());

If the property contains an attribute name (rather than an Expression containing an attribute name) and you want the value from the flow file:

jsonObject.put("hostname", flowFile.getAttribute(context.getProperty(ADD_ATTRIBUTE).getValue()));

If the attribute value itself contains Expression Language and you want to evaluate it, take a look at the following class:

org.apache.nifi.attribute.expression.language.Query

View solution in original post

5 REPLIES 5

Super Guru

If the expression refers to attributes on a flow file, you will need to pass a reference to the flow file into evaluateAttributeExpressions:

FlowFile flowFile = session.get();
jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue());

If the property contains an attribute name (rather than an Expression containing an attribute name) and you want the value from the flow file:

jsonObject.put("hostname", flowFile.getAttribute(context.getProperty(ADD_ATTRIBUTE).getValue()));

If the attribute value itself contains Expression Language and you want to evaluate it, take a look at the following class:

org.apache.nifi.attribute.expression.language.Query

New Contributor

Thank you very much for this answer!

Explorer

Matt, how would the properties set in custom properties file be accessed within the custom controller service's onConfigured method? Thanks.

Super Guru

That sounds like a different issue altogether, perhaps you'd get more/better responses if you create a new question in HCC to deal with it. The short answer is: your ControllerService implementation can have a method annotated with @OnEnabled, looks like you're calling yours onConfigured(). That method can take a ConfigurationContext:

@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {
// Do stuff here 
}

And you can call getProperty(), getProperties(), etc. on it

New Contributor

Matt,

Appreciate if you provide an example on how to use the org.apache.nifi.attribute.expression.language.Query using Groovy. Thanks.

; ;