Nifi expression language for loops over attributes - apache-nifi

Currently I am having around 15 attributes in my flowfile. Out of these 15, i only want a few (all the attributes that have a prefix 'error_' in it. These 'error_*' attributes can have 2 sets of values, eighter- 'valid' or some error code, say- '945'. Now i want to iterate though all the attributes with prefix - 'error_' and if its value is 'valid', do nothing and if its value is having some error code, append the error code to a string separated by ';'. So basically, if I have 5 error_ attributes:
error_field1: '123'
error_field2: 'Valid'
error_field3: '567'
error_field4: 'Valid'
error_field5: '45'
I want my output as - '123;567;45'.
Please help me as i am new to Nifi and i am not sure on how to work with such complex EL.

There are a couple ways to perform this.
${anyMatchingAttribute('error_'):find('\\d+')}
You can use the anyMatchingAttribute() function to evaluate a predicate against multiple attributes, and use the regular expression find() method to check for the presence of digits. This will give you a boolean result, but won't enumerate & join all the values.
${allMatchingAttributes('error_'):join(';'):replaceAll('Valid;', '')}
If you don't need to recall and associate the error codes with the specific field where they were sourced, you can simply concatenate all of the attributes and then use a regular expression to remove the Valid values.

Related

My flow fails for no reason: Invalid Template Language? This is what I do

Team,
Occasionally my flow fails and its enough test it manually to running again. However, I want to avoid that this error ocurrs again to stay in calm.
The error that appears is this:
Unable to process template language expressions in action 'Periodo' inputs at line '0' and column '0': 'The template language function 'split' expects its first parameter to be of type string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#split for usage details.'.
And it appears in 2 of the 4 variables that I create:
Client and Periodo
The variable Clientlooks this:
The same scenario to "Periodo".
The variables are build in the same way:
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'client = '),1)),'indicator')))
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'period = '),1)),'DATA_REPORT_DELIVERY')))
The same scenario to the 4 variables. 4 of them strings (numbers).
Also I attached email example where I extract the info:
CO NIV ICE REFRESCOS DE SOYA has finished successfully.CO NIV ICE REFRESCOS DE SOYA
User
binary.struggle#mail.com
Parameters
output = 7
country = 170
period = 202204012
DATA_REPORT_DELIVERY = NO
read_persistance = YES
write_persistance = YES
client = 18277
indicator_group = SALES
Could you give some help? I reach some attepmpts succeded but it fails for no apparent reason:
Thank you.
I'm not sure if you're interested but I'd do it a slightly different way. It's a little more verbose but it will work and it makes your expressions a lot simpler.
I've just taken two of your desired outputs and provided a solution for those, one being client and the other being country. You can apply the other two as need be given it's the same pattern.
If I take client for example, this is the concept.
Initialize Data
This is your string that you provided in your question.
Initialize Split Lines
This will split up your string for each new line. The expression for this step is ...
split(variables('Data'), '\n')
However, you can't just enter that expression into the editor, you need to do it and then edit in in code view and change it from \\n to \n.
Filter For 'client'
This will filter the array created from the split line step and find the item that contains the word client.
`contains(item(), 'client')`
On the other parallel branches, you'd change out the word to whatever you're searching for, e.g. country.
This should give us a single item array with a string.
Initialize 'client'
Finally, we want to extract the value on the right hand side of the equals sign. The expression for this is ...
trim(split(body('Filter_For_''client''')[0], '=')[1])
Again, just change out the body name for the other action in each case.
I need to put body('Filter_For_''client''')[0] and specify the first item in an array because the filter step returns an array. We're going to assume the length is always 1.
Result
You can see from all of that, you have the value as need be. Like I said, it's a little more verbose but (I think) easier to follow and troubleshoot if something goes wrong.

Xpath multiply formatted output

Have a many entries in an xml file and have xpath with condition:
/XMLReport/Report/PreflightResult/PreflightResultEntry[
#type = 'Check' and #level = 'warning']/PreflightResultEntryMessage/Message/text()
The output is:
onetwothreefour... and more
I need separation
'---' one---two---three---four
or
[enter]
one
two
three
four
Its possible ?
Why you bound XPath expression inside single quote ':
Use this:
string-join(/XMLReport/Report/PreflightResult/PreflightResultEntry[#type = 'Check' and #level = 'warning']/PreflightResultEntryMessage/Message/text(), '---')
Your XPath expression is actually returning a set of text nodes. The way these are displayed depends on the calling application (which you haven't told us anything about). I think your options are (a) change the way the calling application displays the result, or (b) if you're using XPath 2.0+, use the string-join() function to return the result as a string, formatted any way you like within the XPath expression itself.

assistance needed constructing JSONata query

I am trying to construct a JSONata query using the try.jsonata.org Invoice data.
The query I am trying to pose is select distinct OrderID where Order.Product.Price is < 50?
I have not been able to figure out how to do this using the predicate in square brackets notation ... my attempts have been thwarted when I try to get past the $.Account.Order.Product array.
Using $map and $reduce I was able to come up with this rather complex solution ... which still doesn't correctly handle duplicate OrderIDs. (I see that the issue of duplicate removal has been requested here)
Q: What is the proper way to express this query in JSONata?
I think this does what you need:
Account.Order[Product.Price.($ < 50)].OrderID
The expression in the predicate, which gets tested for each Order, will generate an array of Booleans (one for each Product.Price). The resulting predicate will evaluate to true if any of the Booleans within that array are true, due to the semantics of the $boolean function which is implicitly applied.
Overall, the expression will return the OrderID for every Order which has at least one Product whose Price is less than 50

Weka: How to remove instances with missing values

I'm using the Weka application and using a CVS file, I need to remove the instances with missing values. I tried to use the multi filter and use the removevalues filter, but I think I am doing it wrong since it filters ALL my instances. How do I do this right exactly?
To remove instances with missing values from a few attributes you can use weka.filters.unsupervised.instance.SubsetByExpression and use an expression such as
not ismissing(ATT5)
to remove instances with missing values in the attribute with index 5, or
not (ismissing(ATT5) or ismissing(ATT8))
to remove instances with missing values in attributes 5 or 8, and so on.
If you were trying to use the RemoveWithValues filter, it can be done this way but you need to clear the nominalIndices field (removing the -L argument from the filter command) and set a splitPoint value more negative than the minimum value of the attribute being filtered. Otherwise this filter will match any instance whose value matches any of these conditions.
I can't see any obvious way of removing instances that have missing values in any attribute, other than building an expression for SubsetByExpression that checks all of them one by one.

Tibco - Compare values activity

Let's say I have a value that is passed to a process. What activity should i use to compare this value with another?
I know it may seem a foolish question, but i am new to this.
Assuming you are talking about BusinessWorks, comparing text values of XML attributes or elements is done with XPath using the '=' sign. You don't need to use any specific activity to do so. This can actually be done on any branch (with the "Success with condition" switch) or on the input tab of any activity.
For instance, if you want to compare the text values of 2 elements, you can use an XPath formula like this:
$Start/root/myString1 = $Start/root/myString2
This formula returns true if myString1 and myString2 have the same text value, false otherwise.
Then you can, for example, use this formula as a test condition for an "If" or a "Choice" statement on an input tab of any activity.

Resources