pig tutorial - apache pig tutorial - Apache Pig ENDSWITH() - pig latin - apache pig - pig hadoop

What is ENDSWITH() method in Apache Pig ?

  • The endsWith() method determines whether a string ends with the characters of a specified string.
  • This method returns true if the string ends with the characters, and false if not. Note: The endsWith() method is case sensitive.
  • This function accepts two String parameters, it is used to verify whether the first string ends with the second string.


grunt> ENDSWITH(string1, string2)


  • Assume that there is a file named wikitechy_emp.txt in the HDFS directory /pig_data/as shown below. This file contains the employee details such as id, name age and city.


003,Charu,23, London
005, Hansa,22,Bhuwaneshwar 
006, Hena,21,Chennai
007,Robert,24, Bhuwaneshwar
009,Leena,22, Chennai
010,Mahi,22, newyork
011,Priya,23, Tokyo
012,Rahul,20, newyork
  • And, we have loaded this file into Pig with a relation named wikitechy_emp_data as shown below.
grunt> wikitechy_emp_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_emp.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int, city:chararray);
  • Following is an example of ENDSWITH() function, in this example we are verifying, weather the name of every employee ends with the character i.
grunt> emp_endswith = FOREACH wikitechy_emp_data GENERATE (id,name),ENDSWITH ( name, 'i' );
  • The above statement verifies weather the name of the employee ends with the letter i. Since the names of the employees Aadhi,Kali and Mahi ends with the letter i for these two tuples ENDSWITH() function returns the Boolean value ‘true’ and for remaining tuples the value will be ‘false’.
  • The result of the statement will be stored in the relation named emp_endswith. Verify the content of the relation emp_endswith, using the Dump operator as shown below.
grunt> Dump emp_endswith;

((1,Aadav), false)     

Related Searches to Apache Pig ENDSWITH()