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

Expression Language chaining with string manipulation

Expression Language chaining with string manipulation

New Contributor

Newbie here. I'm attempting to utilize multiple string manipulation operations for an attribute that has been passed into one of my processors. Specifically, I have the following where I am referencing an attribute value:

/Absolute/Directory/Path/${path:substringAfter('FruitBasket')}

where the expression could have the hypothetical values of:

"/store/produce/FruitBasket/RedFruits.txt"

"/store/produce/FruitBasket/YellowFruits.txt"

"/store/produce/FruitBasket/OrangeFruits.txt"

I am attempting to replace the ending substring WITHIN the string returned from the expression above as such:

/Absolute/Directory/Path/${path:substringAfter('FruitBasket'):replace('RedFruits.txt', 'Apple1.txt')}

or

/Absolute/Directory/Path/${path:substringAfter('FruitBasket'):replace('YellowFruits.txt', 'Banana2.txt')}

etc..

So that I get:

"/Absolute/Directory/Path/Apple1.txt"

or

"/Absolute/Directory/Path/Banana2.txt"

However, when I do this, I actually get the literal value of:

"/Absolute/Directory/Path/${path:substringAfter('FruitBasket').replace('RedFruits.txt', 'Apple1.txt')}"

Under the section describing the NiFi expression language in https://docs.hortonworks.com, it says we can "chain together multiple function calls". However, I'm unsure of whether this works for string manipulations. Just to check, I reversed the order of chaining and performed the replace BEFORE the substringAfter and still got the same behavior. Additionally, I tried performing the replace alone, and apparently it did not like the embedded numbers. Can anyone shed any light this or present a viable alternative solution? Thank you.

-Jay

2 REPLIES 2

Re: Expression Language chaining with string manipulation

Master Guru

@Jay C

-

What you are trying to do is supported by NiFi Expression Language (EL).

Your "literal value" example output show a "." instead of ":" before replace.

What processor are you using this EL statement in? Not all processor properties support NiFi expression language. Floating your cursor over the small "?" circle next to a property will produce a pop-up that will tell you if EL is supported:

86431-screen-shot-2018-08-09-at-111932-am.png

-

Using the updateAttribute processor you would add a dynamic property with your EL statement as follows:

86429-screen-shot-2018-08-09-at-110845-am.png

-

After the FlowFile passes through the updateAttribute processor and i look at the attributes on the file listed in the output queue, I see:

86430-screen-shot-2018-08-09-at-111417-am.png

-

As you can see the value from the "path" variable on the source FlowFile was used and the output is correct based on how the EL statement was written.
***Note: you may want to include the "/" in your substringAfter or drop the "/" at the end of "/Absolute/Directory/Path/" or instead use the "substringAfterLast('/')" in your EL statement instead to avoid the double "//" in the result.

-

Thanks,

Matt

-

If you found this Answer addressed your original question, please take a moment to login and click "Accept" below the answer.

Re: Expression Language chaining with string manipulation

Master Guru

@Jay C

-

If above did not resolve your issue. Providing some screenshots of your processor configuration may be helpful in pointing out where the issue is.

-

Thanks,

Matt