TIna,

I'm not the greatest Python/Jython developer, nor have I used ExecuteScript
much, but there's a utility [1] that Matt Burgess created to test scripts
that you'd run in ExecuteScript.  Have you had a chance to try running your
script with the utility to debug it?

One thing I noticed is that the last line of your script
(session.transfer(flowFile, REL_SUCCESS)) is not indented, so it can still
be executed if flowFile is null.  A processor can be triggered even when
there are no flowfiles available for it to process, which means a flowfile
might not be present in the ProcessSession.  I think that's what's causing
your error.  Given your current script, using nifi-scrip-tester, I can
reproduce your issue with the NullPointerException being thrown.  After
indenting that line to be part of the previous if-statement,
nifi-script-tester can finish successfully.

Try cloning Matt's nifi-script-tester, build it with gradle, and create a
script.py file with the contents of your script.  Then try running
nifi-script-tester:
java -jar build/libs/nifi-script-tester-1.2.0-all.jar script.py

You'll see a NullPointerException.

Indent the "session.transfer..." line so that it's part of the
if-statement, and run nifi-script-tester again.  You should see:
Flow Files transferred to success: 0

Next step would be to try feeding some of your data into the script.
Running nifi-script-tester without any parameters will print out usage
information.  You can provide a directory with test data to run through
your script with the "-input" parameter.

[1] https://github.com/mattyb149/nifi-script-tester

On Sat, Nov 25, 2017 at 6:31 PM tzhu <[email protected]> wrote:

> The script to process the content of log file:
>
> from org.apache.commons.io import IOUtils
> from java.nio.charset import StandardCharsets
> from org.apache.nifi.processor.io import StreamCallback
>
> class PyStreamCallback(StreamCallback):
>   def __init__(self):
>         pass
>   def process(self, inputStream, outputStream):
>         text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
>         disconnected = text.count("Lost connection to server")
>         connected = text.count("Established connection to server")
>         newFile = 'INSERT INTO [TEST].[db_datawriter].[Test1] \
>        (TOTAL_DISCONNECTIONS,TOTAL_CONNECTIONS)\
>         VALUES (%d, %d)' % (disconnected, connected)
>         outputStream.write(newFile)
>
>
> flowFile = session.get()
> if(flowFile != None):
>     flowFile = session.write(flowFile, PyStreamCallback())
> session.transfer(flowFile, REL_SUCCESS)
>
>
>
> The new script I want to use to write the filename:
>
> from org.apache.commons.io import IOUtils
> from java.nio.charset import StandardCharsets
> from org.apache.nifi.processor.io import StreamCallback
>
> class PyStreamCallback(StreamCallback):
>   def __init__(self):
>         pass
>   def process(self, inputStream, outputStream):
>     fname = flowFile.getAttribute('filename')
>     newFile = 'INSERT INTO [TEST].[db_datawriter].[Test1] \
>     (CLIENT_NAME) VALUES (%d)' % fname
>     outputStream.write(newFile)
>
> flowFile = session.get()
> if(flowFile != None):
>     flowFile = session.write(flowFile, PyStreamCallback())
> session.transfer(flowFile, REL_SUCCESS)
>
>
>
> The error message in the second script shows:
> <http://apache-nifi-developer-list.39713.n7.nabble.com/file/t792/error.jpg
> >
>
> Any idea or help is appreciated!
>
>
>
> --
> Sent from: http://apache-nifi-developer-list.39713.n7.nabble.com/
>

Reply via email to