Apache Pig - STARTSWITH()


Advertisements

This function accepts two string parameters. It verifies whether the first string starts with the second.

Syntax

Given below is the syntax of the STARTSWITH() function.

grunt> STARTSWITH(string, substring)

Example

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

emp.txt

001,Robin,22,newyork
002,BOB,23,Kolkata
003,Maya,23,Tokyo
004,Sara,25,London 
005,David,23,Bhuwaneshwar 
006,Maggy,22,Chennai
007,Robert,22,newyork 
008,Syam,23,Kolkata
009,Mary,25,Tokyo 
010,Saran,25,London 
011,Stacy,25,Bhuwaneshwar 
012,Kelly,22,Chennai 

And, we have loaded this file into Pig with a relation named emp_data as shown below.

grunt > emp_data = LOAD 'hdfs://localhost:9000/pig_data/emp.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int, city:chararray);

Example

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 “Ro”.

grunt> startswith_data = FOREACH emp_data GENERATE (id,name), STARTSWITH (name,’Ro’);

The above statement parses the names of all the employees if any of these names starts with the substring ‘Ro’. Since the names of the employees ‘Robin’ and ‘Robert’ starts with the substring ‘Ro’ 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;
  
((1,Robin),true)
((2,BOB),false)
((3,Maya),false) 
((4,Sara),false)
((5,David),false) 
((6,maggy),false)
((7,Robert),true) 
((8,Syam),false)
((9,Mary),false) 
((10,Saran),false)
((11,Stacy),false) 
((12,Kelly),false)
apache_pig_string_functions.htm
Advertisements