- Apache Pig Tutorial
- Apache Pig - Home
- Apache Pig Introduction
- Apache Pig - Overview
- Apache Pig - Architecture
- Apache Pig Environment
- Apache Pig - Installation
- Apache Pig - Execution
- Apache Pig - Grunt Shell
- Pig Latin
- Pig Latin - Basics
- Load & Store Operators
- Apache Pig - Reading Data
- Apache Pig - Storing Data
- Diagnostic Operators
- Apache Pig - Diagnostic Operator
- Apache Pig - Describe Operator
- Apache Pig - Explain Operator
- Apache Pig - Illustrate Operator
- Grouping & Joining
- Apache Pig - Group Operator
- Apache Pig - Cogroup Operator
- Apache Pig - Join Operator
- Apache Pig - Cross Operator
- Combining & Splitting
- Apache Pig - Union Operator
- Apache Pig - Split Operator
- Pig Latin Built-In Functions
- Apache Pig - Eval Functions
- Load & Store Functions
- Apache Pig - Bag & Tuple Functions
- Apache Pig - String Functions
- Apache Pig - date-time Functions
- Apache Pig - Math Functions
- Other Modes Of Execution
- Apache Pig - User-Defined Functions
- Apache Pig - Running Scripts
- Apache Pig Useful Resources
- Apache Pig - Quick Guide
- Apache Pig - Useful Resources
- Apache Pig - Discussion
Apache Pig - TOMAP()
The TOMAP() function of Pig Latin is used to convert the key-value pairs into a Map.
Syntax
Given below is the syntax of the TOMAP() function.
grunt> TOMAP(key-expression, value-expression [, key-expression, valueexpression ...])
Example
Assume we have a file named employee_details.txt in the HDFS directory /pig_data/, with the following content.
employee_details.txt
001,Robin,22,newyork 002,BOB,23,Kolkata 003,Maya,23,Tokyo 004,Sara,25,London 005,David,23,Bhuwaneshwar 006,Maggy,22,Chennai
We have loaded this file into Pig with the relation name emp_data as shown below.
grunt> emp_data = LOAD 'hdfs://localhost:9000/pig_data/employee_details.txt' USING PigStorage(',') as (id:int, name:chararray, age:int, city:chararray);
Let us now take the name and age of each record as key-value pairs and convert them into map as shown below.
grunt> tomap = FOREACH emp_data GENERATE TOMAP(name, age);
Verification
You can verify the contents of the tomap relation using the Dump operator as shown below.
grunt> DUMP tomap; ([Robin#22]) ([BOB#23]) ([Maya#23]) ([Sara#25]) ([David#23]) ([Maggy#22])
apache_pig_bag_tuple_functions.htm
Advertisements