【问题标题】:How can I pass a variable of type datetime from Pig to python UDF如何将日期时间类型的变量从 Pig 传递给 python UDF
【发布时间】:2016-10-15 01:10:57
【问题描述】:

我的 python UDF 代码,born 是来自 Pig 的日期时间变量, 我尝试将它作为字符串对象,但它也给出了错误, 并将其视为日期时间对象也会出错

from datetime import date

@outputSchema("age_key:chararray")
def agekeyed(born):
    today = date.today()
    return born[:4]

我得到一个错误:

TypeError: 'org.joda.time.DateTime' 对象不可订阅

    at org.python.core.Py.TypeError(Py.java:235)
    at org.python.core.PyObject.__finditem__(PyObject.java:585)
    at org.python.core.PyObjectDerived.__finditem__(PyObjectDerived.java:861)
    at org.python.core.PyObject.__getitem__(PyObject.java:653)
    at org.python.core.PyObjectDerived.__getitem__(PyObjectDerived.java:901)
    at org.python.core.PyObject.__getslice__(PyObject.java:740)
    at org.python.core.PyObjectDerived.__getslice__(PyObjectDerived.java:924)
    at org.python.pycode._pyx3.agekeyed$1(keying.py:6)
    at org.python.pycode._pyx3.call_function(keying.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
    at org.python.core.PyFunction.function___call__(PyFunction.java:376)
    at org.python.core.PyFunction.__call__(PyFunction.java:371)
    at org.python.core.PyFunction.__call__(PyFunction.java:361)
    at org.python.core.PyFunction.__call__(PyFunction.java:356)
    at org.apache.pig.scripting.jython.JythonFunction.exec(JythonFunction.java:117)
    ... 16 more

【问题讨论】:

    标签: python datetime apache-pig udf


    【解决方案1】:

    bornorg.joda.time.DateTime 对象,此处不能应用子字符串。 您需要将 chararray 从 pig 传递给 udf,或者使用它的方法处理对象

    rightnow.monthOfYear().getAsText()
    

    【讨论】:

    • 是的,我使用chararray完成了任务,但是当我使用datetime时,我不能像born.year,born.month这样使用默认的python函数吗?
    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多