猪的python udf错误

时间:2015-03-03 04:13:07

标签: python apache-pig

我想在Pig

中运行python udf
@outputSchema("word:chararray")
def get(s):
    out = s.lower()
    return out;

我收到以下错误:

  File "/home/test.py", line 3, in get
    out = s.lower()
AttributeError: 'NoneType' object has no attribute 'lower'

1 个答案:

答案 0 :(得分:2)

s为空时,您应该处理这种情况。大多数examples such as

from pig_util import outputSchema

@outputSchema('decade:chararray')
def decade(year):
    """
    Get the decade, given a year.

    e.g. for 1998 -> '1990s'
    """
    try:
        base_decade_year = int(year) - (int(year) % 10)
        decade_str = '%ss' % base_decade_year
        print 'input year: %s, decade: %s' % (year, decade_str)
        return decade_str
    except ValueError:
        return None

当值为None时,您需要处理该案例。因此,一种可能的解决方法是尝试:

@outputSchema("word:chararray")
def get(s):
    if s is None:
        return None
    return str(s).lower()