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.). 

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


–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.

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: Logo

You are commenting using your 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