Operators in PIG Latin

103 views Asked by At

I have two values in a PIG Latin script, what should I do to use them mathematically, like if I had two variables

A=(5)
B=(4)

How can I do A+B or something like that ?

2

There are 2 answers

0
Indrajeet Gour On

Yes you can able to do any arithmetic operation in it but the thing is that any variable declaration is kind of different in pig latin -

what you have to do is -

% declare A 5 % declare B 4

and in any of the foreach statement you can use

dummy = foreach column-1,...., column-n, A+B as summation;

this would work.

Let me know if I fulfilled you need.
2
Sivasakthi Jayaraman On

You need to use Foreach stmt to do any mathematical operation, please see the below sample example.

input.txt

2,1
5,3
7,5

Pigscript:

A = LOAD 'input.txt' USING PigStorage(',') AS (val1:int , val1:int); 
B = FOREACH A GENERATE (val1+val2) AS sum, (val1-val2) AS diff;
DUMP B;

Output:

(3,1)
(8,2)
(12,2)