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

What is STARTSWITH() method in Apache Pig ?

  • The startsWith() method determines whether a string begins with the characters of a specified string.
  • This method returns true if the string begins with the characters, and false if not. Note: The startsWith() method is case sensitive.
  • This function accepts two string parameters. It verifies whether the first string starts with the second.


  • Given below is the syntax of the STARTSWITH() function.
grunt> STARTSWITH(string, substring)


  • 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 the STARTSWITH() function. In this example, we have verified whether the names of all the employees start with the substring “Aa”.
grunt> startswith_data = FOREACH wikitechy_emp_data GENERATE (id,name), STARTSWITH (name,’Aa’);
  • The above statement parses the names of all the employees if any of these names starts with the substring ‘Aa’.
  • Since the names of the employees ‘Aadav’ and ‘Aadhi’ starts with the substring ‘Aa’ for these two tuples the STARTSWITH() 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 startswith_data. Verify the content of the relation startswith_data, using the Dump operator as shown below.
grunt> Dump startswith_data;

Related Searches to Apache Pig STARTSWITH()