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.
Syntax:
grunt> ENDSWITH(string1, string2)
Example:
- 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.
wikitechy_emp.txt
001,Aadav,32,Tokyo
002,Aadhi,33,Kolkata
003,Charu,23, London
004,Daya,35,London
005, Hansa,22,Bhuwaneshwar
006, Hena,21,Chennai
007,Robert,24, Bhuwaneshwar
008,Kali,20,Kolkata
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)
((2,Aadhi),true)
((3,Charu),false)
((4,Daya),false)
((5,Hansa),false)
((6,Hena),false)
((7,Robert),false)
((8,Kali),true)
((9,Leena),false)
((10,Mahi),true)
((11,Priya),false)
((12,Rahul),false)