0
votes

Codes in my python script:

import datetime

flowFile = session.get()
if flowFile is not None:
   fltDt = flowFile.getAttribute('FLT_DT')
   dateObj = datetime.datetime.strptime(fltDt, '%Y-%m-%d %H:%M:%S.%f')
   weekday = dateObj.weekday() + 1

   attrMap = {}
   attrMap['WEEKDAY'] = weekday

   for key, value in attrMap.iteritems():
       flowFile = session.putAttribute(flowFile, key, str(value))

   session.transfer(flowFile, REL_SUCCESS)

This simple piece of code runs well with python interpreter as well as in Jython shell. However, when running from Nifi ExecuteScript processor, it always reports error:

2017-07-16 08:17:34,417 ERROR [Timer-Driven Process Thread-9] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=015d1000-4a59-1cf3-2aec-c963c03e3d8b] Failed to process session due to org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: AttributeError: 'java.sql.Timestamp' object has no attribute 'weekday' in <script> at line number 18: {}
org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: AttributeError: 'java.sql.Timestamp' object has no attribute 'weekday' in <script> at line number 18
    at org.apache.nifi.processors.script.ExecuteScript.onTrigger(ExecuteScript.java:230)
    at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1120)
    at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:147)
    at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:47)
    at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:132)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.script.ScriptException: AttributeError: 'java.sql.Timestamp' object has no attribute 'weekday' in <script> at line number 18
    at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:202)
    at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
    at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at org.apache.nifi.script.impl.JythonScriptEngineConfigurator.eval(JythonScriptEngineConfigurator.java:59)
    at org.apache.nifi.processors.script.ExecuteScript.onTrigger(ExecuteScript.java:220)
    ... 11 common frames omitted
Caused by: org.python.core.PyException: null
    at org.python.core.Py.AttributeError(Py.java:205)
    at org.python.core.PyObject.noAttributeError(PyObject.java:1013)
    at org.python.core.PyObject.__getattr__(PyObject.java:1008)
    at org.python.pycode._pyx1359.f$0(<script>:31)
    at org.python.pycode._pyx1359.call_function(<script>)
    at org.python.core.PyTableCode.call(PyTableCode.java:167)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1386)
    at org.python.core.__builtin__.eval(__builtin__.java:497)
    at org.python.core.__builtin__.eval(__builtin__.java:501)
    at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
    at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:40)
    ... 15 common frames omitted

Can anyone help? Much appreciated!

1

1 Answers

0
votes

It looks like the call to datetime.strptime() returns a java.sql.Timestamp object rather than a Python datetime. I found a reference to similar Jython behavior, but not a concrete explanation for why this is.

As a workaround, you might use getDay() from the java.util.Date parent class instead. It's not quite an exact substitute. In Python, weekday() returns Monday as 0, where Java's getDay() returns Sunday as 0.

dateObj = datetime.datetime.strptime(fltDt, '%Y-%m-%d %H:%M:%S.%f')
weekday = dateObj.getDay()