Replace Function in Apache Pig

The purpose of the ‘REPLACE’ function is that it returns the initial argument with all occurrences of the target string replaced by the replacement string. 

This is equivalent to the REPLACE function that is available in most of the RDBMS platforms (MySQL, Oracle & SQL Server) and Big Data Tools (Apache Hive, Apache Impala, etc.). 

Syntax:
REPLACE(string, ‘existing-data’, ‘replaced-data’);

Example:

–Loading the data into a relation
grunt> emp = LOAD ‘Desktop/Docs/emp.csv’ USING PigStorage(‘,’) as (empno:int,ename:chararray,job:chararray,mgr:int,hiredate:chararray,sal:double,comm:double,deptno:int);

–Replacing the column value
grunt> empreplace = FOREACH emp GENERATE empno, ename, REPLACE (ename,’KING’,’Kingsley’);

–Fetch the records from the replaced data.
grunt> dump empreplace;

The following is the output.
(7839,KING,Kingsley)
(7698,BLAKE,BLAKE)
(7782,CLARK,CLARK)
(7566,JONES,JONES)
(7788,SCOTT,SCOTT)
(7902,FORD,FORD)
(7369,SMITH,SMITH)
(7499,ALLEN,ALLEN)
(7521,WARD,WARD)
(7654,MARTIN,MARTIN)
(7844,TURNER,TURNER)
(7876,ADAMS,ADAMS)
(7900,JAMES,JAMES)
(7934,MILLER,MILLER)

Hope you liked this post.

Please click on the follow button to receive updates on the latest posts.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s