Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

default value if an attribute of a flowfile is null in nifi

Solved Go to solution
Highlighted

default value if an attribute of a flowfile is null in nifi

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 .

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: default value if an attribute of a flowfile is null in nifi

Master Guru

@srini

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.

11801-screen-shot-2017-01-26-at-91119-am.png

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

View solution in original post

6 REPLIES 6
Highlighted

Re: default value if an attribute of a flowfile is null in nifi

Expert Contributor

11787-screen-shot-2017-01-25-at-114427-pm.png

you can use the updateAttribute processor. for the grade attribute, use the EL ${grade:replaceNull("nograde")}.

Highlighted

Re: default value if an attribute of a flowfile is null in nifi

Master Guru

@Karthik Narayanan

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".

Highlighted

Re: default value if an attribute of a flowfile is null in nifi

Expert Contributor

yeah , agree with that.

Highlighted

Re: default value if an attribute of a flowfile is null in nifi

Master Guru

@srini

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.

11801-screen-shot-2017-01-26-at-91119-am.png

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

View solution in original post

Highlighted

Re: default value if an attribute of a flowfile is null in nifi

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 .

Highlighted

Re: default value if an attribute of a flowfile is null in nifi

Master Guru

@srini

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

Don't have an account?
Coming from Hortonworks? Activate your account here