Apache Pig - SUBTRACT()



The SUBTRACT() function of Pig Latin is used to subtract two bags. It takes two bags as inputs and returns a bag which contains the tuples of the first bag that are not in the second bag.

Syntax

Given below is the syntax of the SUBTRACT() function.

grunt> SUBTRACT(expression, expression)

Example

Assume that we have two files namely emp_sales.txt and emp_bonus.txt in the HDFS directory /pig_data/ as shown below. The emp_sales.txt contains the details of the employees of the sales department and the emp_bonus.txt contains the employee details who got bonus.

emp_sales.txt

1,Robin,22,25000,sales 
2,BOB,23,30000,sales 
3,Maya,23,25000,sales 
4,Sara,25,40000,sales 
5,David,23,45000,sales 
6,Maggy,22,35000,sales

emp_bonus.txt

1,Robin,22,25000,sales 
2,Jaya,23,20000,admin 
3,Maya,23,25000,sales 
4,Alia,25,50000,admin 
5,David,23,45000,sales 
6,Omar,30,30000,admin

And we have loaded these files into Pig, with the relation names emp_sales and emp_bonus respectively.

grunt> emp_sales = LOAD 'hdfs://localhost:9000/pig_data/emp_sales.txt' USING PigStorage(',')
   as (sno:int, name:chararray, age:int, salary:int, dept:chararray);
	
grunt> emp_bonus = LOAD 'hdfs://localhost:9000/pig_data/emp_bonus.txt' USING PigStorage(',')
   as (sno:int, name:chararray, age:int, salary:int, dept:chararray);	

Let us now group the records/tuples of the relations emp_sales and emp_bonus with the key sno, using the COGROUP operator as shown below.

grunt> cogroup_data = COGROUP emp_sales by sno, emp_bonus by sno;

Verify the relation cogroup_data using the DUMP operator as shown below.

grunt> Dump cogroup_data; 

(1,{(1,Robin,22,25000,sales)},{(1,Robin,22,25000,sales)}) 
(2,{(2,BOB,23,30000,sales)},{(2,Jaya,23,30000,admin)}) 
(3,{(3,Maya,23,25000,sales)},{(3,Maya,23,25000,sales)}) 
(4,{(4,Sara,25,40000,sales)},{(4,Alia,25,50000,admin)}) 
(5,{(5,David,23,45000,sales)},{(5,David,23,45000,sales)}) 
(6,{(6,Maggy,22,35000,sales)},{(6,Omar,30,30000,admin)})

Subtracting One Relation from the Other

Let us now subtract the tuples of emp_bonus relation from emp_sales relation. The resulting relation holds the tuples of emp_sales that are not there in emp_bonus.

grunt> sub_data = FOREACH cogroup_data GENERATE SUBTRACT(emp_sales, emp_bonus);

Verification

Verify the relation sub_data using the DUMP operator as shown below. The emp_sales relation holds the tuples that are not there in the relation emp_bonus.

grunt> Dump sub_data;
   
({})
({(2,BOB,23,30000,sales)})
({})
({(4,Sara,25,40000,sales)})
({})
({(6,Maggy,22,35000,sales)})

In the same way, let us subtract the emp_sales relation from emp_bonus relation as shown below.

grunt> sub_data = FOREACH cogroup_data GENERATE SUBTRACT(emp_bonus, emp_sales);

Verify the contents of the sub_data relation using the Dump operator as shown below.

grunt> Dump sub_data;

({}) 
({(2,Jaya,23,20000,admin)})
({})
({(4,Alia,25,50000,admin)})
({})
({(6,Omar,30,30000,admin)})
apache_pig_eval_functions.htm
Advertisements