You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@nifi.apache.org by KhajaAsmath Mohammed <md...@gmail.com> on 2020/06/03 12:58:17 UTC

Execute Script - Groovy get attribute

Hi,

I have followed the below link to wait for 100 files and then process the
next step.

https://community.cloudera.com/t5/Support-Questions/NiFi-Count-Fileflows-via-attribute/td-p/178860


This works well but my use case is that count number is dynamic. To test
the functionality, I have passed an attribute count to get the value. Below
script is not working. any help?

import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
def trigger=session.get(1)
def flowFiles=session.get(99)
if (!trigger) return

*String attr=trigger.getAttribute("cnt");*


if(!flowFiles || flowFiles.size() < 7)
{
session.rollback()
}
else
{
session.transfer(trigger,REL_SUCCESS)
session.transfer(flowFiles,REL_FAILURE)

}

[image: image.png]

Here is the error I got.

Thanks,
Asmath

Re: Execute Script - Groovy get attribute

Posted by Andy LoPresto <al...@apache.org>.
It also seems like you would rather set the flowfile batch count as a _property_ on the ExecuteScript processor and obtain it from the session, rather than check a specific flowfile for an attribute which may or may not exist. 

Andy LoPresto
alopresto@apache.org
alopresto.apache@gmail.com
He/Him
PGP Fingerprint: 70EC B3E5 98A6 5A3F D3C4  BACE 3C6E F65B 2F7D EF69

> On Jun 3, 2020, at 8:07 AM, Matt Burgess <ma...@apache.org> wrote:
> 
> Asmath,
> 
> When you pass an integer argument into session.get(), you get an ArrayList back, not a single FlowFile. You can just use session.get() to get (up to) 1 FlowFile, or if you call session.get(1) you can just use the first element (once checking to see if it's null), so trigger[0].getAttribute('cnt')
> 
> Regards,
> Matt
> 
> 
> On Wed, Jun 3, 2020 at 8:58 AM KhajaAsmath Mohammed <mdkhajaasmath@gmail.com <ma...@gmail.com>> wrote:
> Hi,
> 
> I have followed the below link to wait for 100 files and then process the next step.
> 
> https://community.cloudera.com/t5/Support-Questions/NiFi-Count-Fileflows-via-attribute/td-p/178860 <https://community.cloudera.com/t5/Support-Questions/NiFi-Count-Fileflows-via-attribute/td-p/178860> 
> 
> This works well but my use case is that count number is dynamic. To test the functionality, I have passed an attribute count to get the value. Below script is not working. any help? 
> 
> import org.apache.commons.io.IOUtils
> import java.nio.charset.StandardCharsets
> def trigger=session.get(1)
> def flowFiles=session.get(99)
> if (!trigger) return
> 
> String attr=trigger.getAttribute("cnt");
> 
> 
> if(!flowFiles || flowFiles.size() < 7)
> {
> session.rollback()
> }
> else
> {
> session.transfer(trigger,REL_SUCCESS)
> session.transfer(flowFiles,REL_FAILURE)
> 
> }
> 
> <image.png>
> 
> Here is the error I got.
> 
> Thanks,
> Asmath 


Re: Execute Script - Groovy get attribute

Posted by KhajaAsmath Mohammed <md...@gmail.com>.
Thanks Matt. This worked.

On Wed, Jun 3, 2020 at 10:08 AM Matt Burgess <ma...@apache.org> wrote:

> Asmath,
>
> When you pass an integer argument into session.get(), you get an ArrayList
> back, not a single FlowFile. You can just use session.get() to get (up to)
> 1 FlowFile, or if you call session.get(1) you can just use the first
> element (once checking to see if it's null), so
> trigger[0].getAttribute('cnt')
>
> Regards,
> Matt
>
>
> On Wed, Jun 3, 2020 at 8:58 AM KhajaAsmath Mohammed <
> mdkhajaasmath@gmail.com> wrote:
>
>> Hi,
>>
>> I have followed the below link to wait for 100 files and then process the
>> next step.
>>
>>
>> https://community.cloudera.com/t5/Support-Questions/NiFi-Count-Fileflows-via-attribute/td-p/178860
>>
>>
>> This works well but my use case is that count number is dynamic. To test
>> the functionality, I have passed an attribute count to get the value. Below
>> script is not working. any help?
>>
>> import org.apache.commons.io.IOUtils
>> import java.nio.charset.StandardCharsets
>> def trigger=session.get(1)
>> def flowFiles=session.get(99)
>> if (!trigger) return
>>
>> *String attr=trigger.getAttribute("cnt");*
>>
>>
>> if(!flowFiles || flowFiles.size() < 7)
>> {
>> session.rollback()
>> }
>> else
>> {
>> session.transfer(trigger,REL_SUCCESS)
>> session.transfer(flowFiles,REL_FAILURE)
>>
>> }
>>
>> [image: image.png]
>>
>> Here is the error I got.
>>
>> Thanks,
>> Asmath
>>
>

Re: Execute Script - Groovy get attribute

Posted by Matt Burgess <ma...@apache.org>.
Asmath,

When you pass an integer argument into session.get(), you get an ArrayList
back, not a single FlowFile. You can just use session.get() to get (up to)
1 FlowFile, or if you call session.get(1) you can just use the first
element (once checking to see if it's null), so
trigger[0].getAttribute('cnt')

Regards,
Matt


On Wed, Jun 3, 2020 at 8:58 AM KhajaAsmath Mohammed <md...@gmail.com>
wrote:

> Hi,
>
> I have followed the below link to wait for 100 files and then process the
> next step.
>
>
> https://community.cloudera.com/t5/Support-Questions/NiFi-Count-Fileflows-via-attribute/td-p/178860
>
>
> This works well but my use case is that count number is dynamic. To test
> the functionality, I have passed an attribute count to get the value. Below
> script is not working. any help?
>
> import org.apache.commons.io.IOUtils
> import java.nio.charset.StandardCharsets
> def trigger=session.get(1)
> def flowFiles=session.get(99)
> if (!trigger) return
>
> *String attr=trigger.getAttribute("cnt");*
>
>
> if(!flowFiles || flowFiles.size() < 7)
> {
> session.rollback()
> }
> else
> {
> session.transfer(trigger,REL_SUCCESS)
> session.transfer(flowFiles,REL_FAILURE)
>
> }
>
> [image: image.png]
>
> Here is the error I got.
>
> Thanks,
> Asmath
>