pig tutorial - apache pig tutorial - Apache Pig INDEXOF() - pig latin - apache pig - pig hadoop
What is INDEXOF() in Apache Pig ?
- The indexOf() method returns the position of the first occurrence of a specified value in a string.
- This method returns -1 if the value to search for never occurs. Note: The indexOf() method is case sensitive. Tip: Also look at the lastIndexOf() method.
- The INDEXOF() function accepts a string value, a character and an index (integer). It returns the first occurrence of the given character in the string, searching forward from the given index.
Syntax:
- Given below is the syntax of the INDEXOF() function.
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
- And, we have loaded this file into Pig with a relation named wikitechy_emp_data as shown below.
- Given below is an example of the INDEXOF() function. In this example, we are finding the occurrence of the letter 'r' in the names of every employee using this function.
- The above statement parses the name of each employee and returns the index value at which the letter ‘r’ occurred for the first time. If the name doesn’t contain the letter ‘r’ it returns the value -1
- The result of the statement will be stored in the relation named indexof_data. Verify the content of the relation indexof_data, using the Dump operator as shown below.