Surender Raja
Surender Raja

Reputation: 3599

How Replace function in Pig works?

My input file name is words.txt as below . Also there is no space in each record of this below file .

 Hi
 Hi
 How

I am loading this file into Pig

words = LOAD '/user/inputs/words.txt' USING PigStorage() AS (line:chararray);

words_each = FOREACH words GENERATE REPLACE(line,'','|') ;

dump words_each;

I am getting output as

 |H|i|
 |H|i|
 |H|o|w|

But I would like to know how exactly REPLACE functions treats '' which is my second argument in REPLACE function .

There is no empty space in my file, then how come I am getting | in my output .

Upvotes: 0

Views: 259

Answers (1)

Nishu Tayal
Nishu Tayal

Reputation: 20830

Well, As per your statement, REPLACE function is called on ''. It doesn't contain any whitespace.
If you want to replace the space, you need to give it like this ' '. +

Both are different conditions as given below:

words_each = FOREACH words GENERATE REPLACE(line,'','|') ; // without space
words_each = FOREACH words GENERATE REPLACE(line,' ','|') ; // with space

First condition will add the Pipe symbol(|) after each character, while 2nd condition won't make any impact because there is no space in your file content.

Upvotes: 2

Related Questions