Apache Pig - TRIM()


Advertisements

The TRIM() function accepts a string and returns its copy after removing the unwanted spaces before and after it.

Syntax

Here is the syntax of the TRIM() function.

grunt> TRIM(expression)

Example

Assume we have some unwanted spaces before and after the names of the employees in the records of the emp_data relation.

grunt> Dump emp_data; 
 
(1, Robin ,22,newyork)
(2,BOB,23,Kolkata) 
(3, Maya ,23,Tokyo)
(4,Sara,25,London)
(5, David ,23,Bhuwaneshwar) 
(6,maggy,22,Chennai)
(7,Robert,22,newyork) 
(8, Syam ,23,Kolkata)
(9,Mary,25,Tokyo) 
(10, Saran ,25,London)
(11, Stacy,25,Bhuwaneshwar)
(12, Kelly ,22,Chennai)

Using the TRIM() function, we can remove these heading and tailing spaces from the names, as shown below.

grunt> trim_data = FOREACH emp_data GENERATE (id,name), TRIM(name);

The above statement returns the copy of the names by removing the heading and tailing spaces from the names of the employees. The result is stored in the relation named trim_data. Verify the result of the relation trim_data using the Dump operator as shown below.

grunt> Dump trim_data;
  
((1, Robin ),Robin)
((2,BOB),BOB)
((3, Maya ),Maya)
((4,Sara),Sara)
((5, David ),David)
((6,maggy),maggy) 
((7,Robert),Robert)
((8, Syam ),Syam) 
((9,Mary),Mary)
((10, Saran ),Saran)
((11, Stacy),Stacy)
((12, Kelly ),Kelly)
apache_pig_string_functions.htm
Advertisements