Support Questions

Find answers, ask questions, and share your expertise

Escaping forward slash

avatar
Rising Star

Hi 

I will like to append forward slash / conditionally in EL. But not able to acheive it.

${extracted.path:length():gt(0):ifElse(${extracted.path:append('/'):append(${filename})},${filename})}

Have tried using ${literal('/')} as well but its not getting the value.

 

Thanks

 

3 REPLIES 3

avatar
Master Mentor

@nifier 

The NiFi EL you are using is valid and works.  So this raise the question as to where you are trying to use it?

  1. What version of Apache NiFi are you using?
  2. Which NiFi processor are you using this in?
  3. Which Processor property are you using it in? 

    NOTE: Make sure the processor property supports NiFi EL.  You can't use EL in every property.

I validated your EL using the UpdateAttribute processor.

I do have another question about your ifElse:

  • Why are you appending a filename to path?

A more typical approach would be t simply use ${extracted.path}/${filename} in the processor that writes file out to destination.  If extracted.path is empty or does not exist it returns nothing.

Please help our community grow. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt

 

avatar
Rising Star

hi @MattWho 

  1. What version of Apache NiFi are you using? 2.1.0
  2. Which NiFi processor are you using this in? UpdateAttribute
  3. Which Processor property are you using it in? Added "extracted.path" Property

Our use case is to retain the same nested folder structure as the source. 

nifier_0-1744311075054.png

 

AS you mentioned I did use "${extracted.path}/${filename}", however in case of "demo109.txt" the "extracted.path" is empty and gets created as "/${filename}" with forward slash at the begining.

To avoid this we wanted to set "/" conditionally.

 

Thanks

avatar
Master Mentor

@nifier 
I am not seeing the same behavior as you have reported which make me think your extracted.path is not not really 0 bytes.

I suggest adding another dynamic property to your update Attribute processor to output the calculated length to verify.

Here is what i see after my UpdateAttribute processor (newdir is set using your shared NiFi Expression Language (NEL) statement).

MattWho_0-1744375216155.png

So I created  extracted.path with a single white space and then see what you describe, but that whitespace is a byte, so expected output:

MattWho_1-1744375445639.png

If it turns out your problem is because you have white space in the Extracted.path attribute, you could modify your NEL statement as follows:

${extracted.path:trim():length():gt(0):ifElse(${extracted.path:append('/'):append(${filename})},${filename})}

You use the trim() NEL function to remove leading or trialing whitespace from the extracted.path before calculating length.  This includes trimming of a line return.

So in below you will see desired output even though extracted.path length is not 0 because I trimmed white space or line return.

MattWho_2-1744375977531.png

Please help our community grow. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt