Execute Script - Groovy get attribute

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Execute Script - Groovy get attribute

KhajaAsmath Mohammed
Hi,

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


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 
Reply | Threaded
Open this post in threaded view
|

Re: Execute Script - Groovy get attribute

Matt Burgess-2
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 <[hidden email]> wrote:
Hi,

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


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 
Reply | Threaded
Open this post in threaded view
|

Re: Execute Script - Groovy get attribute

Andy LoPresto
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
[hidden email]
[hidden email]
He/Him
PGP Fingerprint: 70EC B3E5 98A6 5A3F D3C4  BACE 3C6E F65B 2F7D EF69

On Jun 3, 2020, at 8:07 AM, Matt Burgess <[hidden email]> 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 <[hidden email]> wrote:
Hi,

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


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 

Reply | Threaded
Open this post in threaded view
|

Re: Execute Script - Groovy get attribute

KhajaAsmath Mohammed
In reply to this post by Matt Burgess-2
Thanks Matt. This worked.

On Wed, Jun 3, 2020 at 10:08 AM Matt Burgess <[hidden email]> 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 <[hidden email]> wrote:
Hi,

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


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