Andrew Ehrlich
Andrew Ehrlich

Reputation: 287

Pig: positionals counting from right?

I have a tuple in my pig script:

((,v1,fb,fql))

I know I can choose elements from the left as $0 (blank), $1 ("v1"), etc. But can I choose elements from the right? The tuples will be different lengths, but I would always like to get the last element.

Upvotes: 2

Views: 41

Answers (1)

mr2ert
mr2ert

Reputation: 5184

You can't. You can however write a python UDF to extract it:

# 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)]

And used like:

register 'myudf.py' using jython as pythonUDFs ;

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

Upvotes: 2

Related Questions