猪:位置从右计数?

时间:2014-04-01 00:20:10

标签: apache-pig

我的猪脚本中有一个元组:

((,v1,fb,fql))

我知道我可以选择左边的元素为$ 0(空白),$ 1(“v1”)等等。但是我可以从右边选择元素吗?元组的长度不同,但我总是想得到最后一个元素。

1 个答案:

答案 0 :(得分:2)

你不能。但是你可以编写一个python UDF来提取它:

# Make sure to include the appropriate ouputSchema
def getFromBack(TUPLE, pos):
    # gets elements from the back of TUPLE 
    # You can also do TUPLE[-pos], but this way it starts at 0 which is closer
    # to Pig
    return TUPLE[::-1][pos]

    # This works the same way as above, but may be faster
    # return TUPLE[-(pos + 1)]

并使用如下:

register 'myudf.py' using jython as pythonUDFs ;

B = FOREACH A GENERATE pythonUDFs.getFromBack(T, 0) ;