0

My python UDF code, born is a datetime variable from Pig , I tried it as string object but it also gave an error, and treating it as a datetime object also gave an error

from datetime import date

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

I get an error :

TypeError: 'org.joda.time.DateTime' object is unsubscriptable

    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
pratiklodha
  • 1,095
  • 12
  • 20

1 Answers1

0

born is a org.joda.time.DateTime object, and substring cannot be applied here. You need to pass a chararray from pig to the udf, or deal with the object with it's methods

rightnow.monthOfYear().getAsText()
54l3d
  • 3,913
  • 4
  • 32
  • 58
  • Yes, I achieved the task using a chararray, But when I used datetime, can't I use the default python function on it like born.year,born.month ? – pratiklodha Jun 14 '16 at 14:43