pig tutorial - apache pig tutorial - Apache Pig CONCAT() Function - pig latin - apache pig - pig hadoop




What is CONCAT() Function in Apache Pig ?

  • The CONCAT() function is used to concatenate two strings to form a single string.
  • The CONCAT() function concatenates two or more expressions together.
  • The CONCAT() function returns a string that is the result of concatenating two or more string values.
  • The CONCAT() function is used to add two or more strings.
  • The CONCAT() function which is used in Apache Pig is used to concatenate two or more expressions of the same type.
 Apache Pig Concat() Function

Learn Apache Pig - Apache Pig tutorial - Apache Pig Concat() Function - Apache Pig examples - Apache Pig programs

Syntax

grunt> CONCAT (expression, expression, [...expression])

Example

wikitechy_employee_details.txt

001,Suresh,Reddy,21,9848022337,Hyderabad,89
002,john,Battacharya,22,9848022338,Kolkata,78 
003,Aanchal,Khanna,22,9848022339,Delhi,90 
004,Preethi,Agarwal,21,9848022330,Pune,93 
005,Sruti,Mohanthy,23,9848022336,Bhuwaneshwar,75 
006,Vanitha,Mishra,23,9848022335,Chennai,87 
007,Kamal,Nayak,24,9848022334,trivendram,83 
008,Sabrina,Nambiayar,24,9848022333,Chennai,72
  • We have loaded this file wikitechy_employee_details.txt into Pig with the relation named wikitechy_employee_details as shown below.
grunt> wikitechy_employee_details = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_employee_details.txt' USING PigStorage(',')
   as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray, gpa:int);

Concatenating Two Strings

Method 1:

  • We have loaded this file wikitechy_employee_details.txt into Pig with the relation named wikitechy_employee_details as shown below.
  • We need to verify the contents of the wikitechy_employee_details relation by using the Dump operator which is given below:
grunt> Dump  wikitechy_employee_details;
( 1,Suresh,Reddy,21,9848022337,Hyderabad,89 ) 
( 2,john,Battacharya,22,9848022338,Kolkata,78 )
( 3,Aanchal,Khanna,22,9848022339,Delhi,90 ) 
( 4,Preethi,Agarwal,21,9848022330,Pune,93 )
( 5,Sruti,Mohanthy,23,9848022336,Bhuwaneshwar,75 )
( 6,Vanitha,Mishra,23,9848022335,Chennai,87 )
( 7,Kamal,Nayak,24,9848022334,trivendram,83 )
( 8,Sabrina,Nambiayar,24,9848022333,Chennai,72 )
  • We need to verify the schema by using the describe operator as shown below
grunt> Describe wikitechy_employee_details;
wikitechy_employee_details: {id: int, firstname: chararray, lastname: chararray, age: int,
   phone: chararray, city: chararray, gpa: int}
  • In the schema which is given above by using the describe operator, we observe the name of the employee is represented using two chararray values namely firstname and lastname.
grunt> employee_name_concat = foreach wikitechy_employee_details Generate CONCAT (firstname, lastname);

Verification

grunt> Dump employee_name_concat;

Method 2:

  • We can use an optional delimiter which is used between the two expressions which is given below:
grunt> CONCAT(firstname, '_',lastname);
  • We need to concatenate the first name and last name of the employee records in the wikitechy_employee_details relation by placing ‘_’ between them which is given below
grunt> employee_name_concat = foreach wikitechy_employee_details GENERATE CONCAT(firstname, '_',lastname); 

Verification

grunt> Dump employee_name_concat;

Output

 (Suresh_Reddy) 
(john_Battacharya) 
(Aanchal_Khanna) 
(Preethi_Agarwal) 
(Sruti_Mohanthy) 
(Vanitha_Mishra) 
(Kamal_Nayak) 
(Sabrina_Nambiayar)

Related Searches to Apache Pig CONCAT() Function