the foreach operator is used to generate specified data transformations based on the column data.
syntax
given below is the syntax of foreach operator.
grunt> relation_name2 = foreach relatin_name1 generate (required data);
example
assume that we have a file named student_details.txt in the hdfs directory /pig_data/ as shown below.
student_details.txt
001,rajiv,reddy,21,9848022337,hyderabad 002,siddarth,battacharya,22,9848022338,kolkata 003,rajesh,khanna,22,9848022339,delhi 004,preethi,agarwal,21,9848022330,pune 005,trupthi,mohanthy,23,9848022336,bhuwaneshwar 006,archana,mishra,23,9848022335,chennai 007,komal,nayak,24,9848022334,trivendram 008,bharathi,nambiayar,24,9848022333,chennai
and we have loaded this file into pig with the relation name student_details as shown below.
grunt> student_details = load 'hdfs://localhost:9000/pig_data/student_details.txt' using pigstorage(',')
as (id:int, firstname:chararray, lastname:chararray,age:int, phone:chararray, city:chararray);
let us now get the id, age, and city values of each student from the relation student_details and store it into another relation named foreach_data using the foreach operator as shown below.
grunt> foreach_data = foreach student_details generate id,age,city;
verification
verify the relation foreach_data using the dump operator as shown below.
grunt> dump foreach_data;
output
it will produce the following output, displaying the contents of the relation foreach_data.
(1,21,hyderabad) (2,22,kolkata) (3,22,delhi) (4,21,pune) (5,23,bhuwaneshwar) (6,23,chennai) (7,24,trivendram) (8,24,chennai)