我的python脚本中的代码:
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)
这段简单的代码可以很好地运行python解释器以及Jython shell。但是,从Nifi ExecuteScript处理器运行时,它始终报告错误:
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
有人可以帮忙吗?非常感谢!
答案 0 :(得分:0)
看起来对{{1}}的调用返回java.sql.Timestamp对象而不是Python日期时间。我找到了groupby
,但没有具体解释为什么会这样。
作为一种解决方法,您可以使用java.util.Date父类中的GroupBy.apply
。它不是一个完全替代品。在Python中,reference to similar Jython behavior将星期一作为0返回,其中Java的getDay()
将星期日作为0返回。
{{1}}