how to create a small constant relation(table) in pig?
Asked Answered
A

3

5

is there a way to create a small constant relation(table) in pig? I need to create a relation with only 1 tuple that contains constant values. something along the lines of:

A = LOAD using ConstantLoader('{(1,2,3)}');

thanks, Ido

Attribution answered 16/11, 2012 at 9:56 Comment(3)
What are you trying to accomplish? And why couldn't you accomplish it by using %DECLARE or passing in several parameters which contain these constants?Anastomosis
I'm using a macro that demands a relation as a parameter, and I want to pass an empty relationAttribution
I need the same. This is related to https://mcmap.net/q/838625/-define-tuple-datas-in-the-pig-scriptUnanimity
U
1

Fast answer: no.

I asked about it in pig-dev mailing list.

Unanimity answered 14/12, 2014 at 0:4 Comment(0)
J
2

I'm not sure why you would need that but, here's an ugly solution:

A = LOAD 'some/sample/file' ;
B = FOREACH A GENERATE '' ;
C = LIMIT A 1 ;

Now, you can use 'C' as the 'empty relation' that has one empty tuple.

Janise answered 27/11, 2014 at 12:57 Comment(1)
thanks, indeed ugly :) note that it would get uglier if you want a relation with 2 records, say {(1,2)(3,4)}Attribution
F
2
DEFINE GenerateRelationFromString(string) RETURNS relation {
    temp = LOAD 'somefile';
    tempLimit1 = LIMIT temp 1;
    $relation = FOREACH tempLimit1 GENERATE FLATTEN(TOKENIZE('$string', ','));
};

usage:

fourRows = GenerateRelationFromString('1,2,3,4');
myConstantRelation = FOREACH fourRows GENERATE ( 
CASE $0
    WHEN '1' THEN (1, 'Ivan')
    WHEN '2' THEN (2, 'Boris')
    WHEN '3' THEN (3, 'Vladimir')
    WHEN '4' THEN (4, 'Olga')
END
) as myTuple;

This for sure is hacky, and the right way, in my mind, would be to implement a StringLoader() that would work like this:

fourRows = LOAD '1,2,3,4' USING StringLoader(',');

The argument typically used for file location would just be used as litral string input.

Financier answered 6/3, 2017 at 19:0 Comment(0)
U
1

Fast answer: no.

I asked about it in pig-dev mailing list.

Unanimity answered 14/12, 2014 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.