Created 01-26-2017 03:58 AM
IS there any way to assign a default value if an attribute of a flowfile is NULL or empty in Nifi flow .
ex: We are getting realtime streaming data (ename, empNO , salary , grade,branch) , now and then We are getting null values for the grade and branch fields . We would like to assign 'nograde','branch-none' values to grade,branch attributes in Nifi processor . Please let us know the way . Thanks in advance .
Created on 01-26-2017 02:16 PM - edited 08-18-2019 06:15 AM
NULL and empty are two different things and need to handled in two different ways.
NULL indicates there is no value set, while EMPTY means a value of type new line, carriage return, tab, or space has been set as the value.
There are a number of NiFi Expression language functions that work with non-exist attributes and attributes with either null or empty values.
First question to ask is whether every FlowFile has both the branch and grade attributes? If some are missing the attributes, different handling needs to be done. If you don't know for sure that every FlowFile will have these attributes created, I suggest using the "isEmpty" NiFi expression language function to check. The "isEmpty" function will return true if the attributes does not exist, exist containing no value or exist containing only new line, carriage return, tab, or space.
The UpdateAttribute has an "Advanced" UI that allows you to create if/then type functions.
So With the above rules applied, all FlowFile leaving this UpdateAttribute processor will have both the "branch" and "grade" attributes. Those attributes will either have some previously assigned non-empty value or the "nobranch" and/or "nograde" values assigned to them. This works even if the incoming FlowFile was missing these attributes.
Thanks,
Matt
Created on 01-26-2017 04:40 AM - edited 08-18-2019 06:15 AM
you can use the updateAttribute processor. for the grade attribute, use the EL ${grade:replaceNull("nograde")}.
Created 01-26-2017 02:27 PM
The only problem with the "replaceNull" function is that it will only return either "nograde" or "nobranch" if the incoming FlowFile does not have have an attribute with the name "grade" or " branch". If they exist but have no value set, the result will still be set to no value instead of being assigned "nograde" or "nobranch".
Created 01-26-2017 05:18 PM
yeah , agree with that.
Created on 01-26-2017 02:16 PM - edited 08-18-2019 06:15 AM
NULL and empty are two different things and need to handled in two different ways.
NULL indicates there is no value set, while EMPTY means a value of type new line, carriage return, tab, or space has been set as the value.
There are a number of NiFi Expression language functions that work with non-exist attributes and attributes with either null or empty values.
First question to ask is whether every FlowFile has both the branch and grade attributes? If some are missing the attributes, different handling needs to be done. If you don't know for sure that every FlowFile will have these attributes created, I suggest using the "isEmpty" NiFi expression language function to check. The "isEmpty" function will return true if the attributes does not exist, exist containing no value or exist containing only new line, carriage return, tab, or space.
The UpdateAttribute has an "Advanced" UI that allows you to create if/then type functions.
So With the above rules applied, all FlowFile leaving this UpdateAttribute processor will have both the "branch" and "grade" attributes. Those attributes will either have some previously assigned non-empty value or the "nobranch" and/or "nograde" values assigned to them. This works even if the incoming FlowFile was missing these attributes.
Thanks,
Matt
Created 01-26-2017 05:17 PM
Thanks for your guidelines @Matt
I am sure each flow file is having grade and branch attributes . as you said , We are getting NULL(no value set) values not empty for branch and grade
in my case ,We have 3 conditions in which data will be :
1. sometimes we are getting NULL value for grade , not for branch .
2.sometimes , NULL for branch ,some value for grade .
3. sometimes both fields are getting NULL values .
ex : ename, eno , salary , branch ,grade
srini,1,10000,branch-A,
sai,2,2000,,grade-A
sai,3,,
As i understood , conditions are wokr with 'AND' caluse , will above solution work for my case ?
Thanks in Advance .
Created 01-26-2017 05:36 PM
The default "FlowFIle Policy" is "use clone" within the advanced UI. For an "and" operation, you will want change that to "use original". Just keep in mind (does not apply here since both rules are unique) that if more then one rule tries to perform the exact same "Actions", only the last rule in the list that will be applied to the original FlowFile on output.
Both "Rules" are run against every FlowFile that passes through this UpdateAttribute processor, so it will work in cases where only one or the other is set and cases where both are not set. Rules are operated on in the order they are listed. You can drag rules up and down in the list to order them how you like.
If this answer addresses your question, please click "accept".
Thank you,
Matt