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


  • The LAST_INDEX_OF() function accepts a string value and a character.
  • It returns the last occurrence of the given character in the string, searching backward from the end of the string.


  • Given below is the syntax of the LAST_INDEX_OF() function.
      grunt> LAST_INDEX_OF(string, 'character')


  • 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);
  • Given below is an example of the LAST_INDEX_OF() function. In this example, we are going to find the occurrence of the letter 'e' from the end, in the names of every employee.
grunt> last_index_data = FOREACH wikitechy_emp_data GENERATE (id,name), LAST_INDEX_OF(name, 'e');
  • The above statement parses the name of each employee from the end and returns the index value at which the letter ‘e’ occurred for the first time. If the name doesn’t contain the letter ‘e’ it returns the value −1
  • The result of the statement will be stored in the relation named last_index_data. Verify the content of the relation last_index_data using the Dump operator as shown below.
grunt> Dump last_index_data;


  • ((1,Aadav),-1)
  • ((2,Aadhi),-1)
  • ((3,Charu),-1)
  • ((4,Daya),-1)
  • ((5,Hansa),-1)
  • ((6,Hena),1)
  • ((7,Robert),3)
  • ((8,Kali),-1)
  • ((9,Leena),2)
  • ((10,Mahi),-1)
  • ((11,Priya),1)
  • ((12,Rahul),-1)

  • Related Searches to Apache Pig LAST_INDEX_OF()