pig tutorial - apache pig tutorial - Apache Pig - REPLACE() - pig latin - apache pig - pig hadoop

What is REPLACE()?

  • REPLACE() function is used to replace all the characters in a given string with the new characters.


Given below is the syntax of the REPLACE() function. This function accepts three parameters, namely,

  • string − The string that is to be replaced. If we want to replace the string within a relation, we have to pass the column name the string belongs to.
  • regEXP − Here we have to pass the string/regular expression we want to replace.
  • newChar − Here we have to pass the new value of the string.
grunt> REPLACE(string, 'regExp', 'newChar');


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.



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

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

Following is an example of the REPLACE() function. In this example, we have replaced the name of the city Bhuwaneshwar with a shorter form Bhuw.

grunt> replace_data = FOREACH wikitechy_emp_data GENERATE (id,city),REPLACE(city,'Bhuwaneshwar','Bhuw');

The above statement replaces the string 'Bhuwaneshwar' with 'Bhuw' in the column named city in the wikitechy_emp_data relation and returns the result. This result is stored in the relation named replace_data. Verify the content of the relation replace_data using the Dump operator as shown below.

grunt> Dump replace_data;


Related Searches to Apache Pig - REPLACE()

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.