pig tutorial - apache pig tutorial - Apache Pig TOMAP() - pig latin - apache pig - pig hadoop
What is TOMAP() function in Apache Pig ?
- The TOMAP() function is used to converts key/value expression pairs into a map.
- Needs an even number of expressions as parameters.
- The elements must comply with map type rules.

Learn apache pig - apache pig tutorial - apache pig tomap() - apache pig examples - apache pig programs
Syntax
grunt> TOMAP(key-expression, value-expression [, key-expression, valueexpression ...])
Example
- Ensure that we have a file named wikitechy_emp_details.txt in the HDFS directory /pig_data/, as given below.
Wikitechy_emp_details.txt
111,Anu,22,newyork
112,Bastin,23,Kolkata
113,Cimen,23,Tokyo
114,Darathy,25,London
115,Enba,23,Bhuwaneshwar
116,Favin,22,Chennai
- You have loaded this file into Pig with the relation name emp_data as given below.
grunt> emp_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_emp_details.txt' USING PigStorage(',')
as (id:int, name:chararray, age:int, city:chararray);
- · You can take the name and age of each record as key-value pairs and convert them into map as given below.
grunt> tomap = FOREACH emp_data GENERATE TOMAP(name, age);
Verification
- · Now you can verify the contents of the tomap relation using the Dump operator as given below.
grunt> DUMP tomap;
Output
- The above statement stores the result in the relation named emp_data.
([Anu#22])
([Bastin#23])
([Cimen#23])
([Darathy#25])
([Enba#23])
([Favin#22])