You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pig.apache.org by Marco Cadetg <ma...@zattoo.com> on 2011/10/11 09:51:32 UTC

replace value of a given field

Hi there,

I would like to replace the value of a field based on its value. E.g.:

A = LOAD 'student' USING PigStorage() AS (name:chararray);
DUMP A;
(John)
(Mary
(Bill)
(Joe)
(John)

Now I would like to replace all John's with Marco. Is there a way to do this
in PIG? I thought about using sth like SPLIT IF John and then union the
result again. Is there a better way?

Cheers
-Marco

Re: replace value of a given field

Posted by Marco Cadetg <ma...@zattoo.com>.
Hi Andrew,

Thanks a lot that is exactly what I was looking for.

Greets,
-Marco

On Tue, Oct 11, 2011 at 9:58 AM, Andrew Clegg <andrew.clegg+mahout@gmail.com
> wrote:

> Try:
>
> foreach A generate (name == 'John' ? 'Marco' : name) as name;
>
> or for multiple:
>
> foreach A generate
> (name == 'John' ? 'Marco' :
> (name == 'Sally' ? 'Anne' : name)) as name;
>
> Maybe there's a better way if you have to do lots of these at once..?
>
> See also REPLACE for replacing a substring.
>
> On 11 October 2011 08:51, Marco Cadetg <ma...@zattoo.com> wrote:
> > Hi there,
> >
> > I would like to replace the value of a field based on its value. E.g.:
> >
> > A = LOAD 'student' USING PigStorage() AS (name:chararray);
> > DUMP A;
> > (John)
> > (Mary
> > (Bill)
> > (Joe)
> > (John)
> >
> > Now I would like to replace all John's with Marco. Is there a way to do
> this
> > in PIG? I thought about using sth like SPLIT IF John and then union the
> > result again. Is there a better way?
> >
> > Cheers
> > -Marco
> >
>
>
>
> --
>
> http://tinyurl.com/andrew-clegg-linkedin | http://twitter.com/andrew_clegg
>

Re: replace value of a given field

Posted by Andrew Clegg <an...@gmail.com>.
Try:

foreach A generate (name == 'John' ? 'Marco' : name) as name;

or for multiple:

foreach A generate
(name == 'John' ? 'Marco' :
(name == 'Sally' ? 'Anne' : name)) as name;

Maybe there's a better way if you have to do lots of these at once..?

See also REPLACE for replacing a substring.

On 11 October 2011 08:51, Marco Cadetg <ma...@zattoo.com> wrote:
> Hi there,
>
> I would like to replace the value of a field based on its value. E.g.:
>
> A = LOAD 'student' USING PigStorage() AS (name:chararray);
> DUMP A;
> (John)
> (Mary
> (Bill)
> (Joe)
> (John)
>
> Now I would like to replace all John's with Marco. Is there a way to do this
> in PIG? I thought about using sth like SPLIT IF John and then union the
> result again. Is there a better way?
>
> Cheers
> -Marco
>



-- 

http://tinyurl.com/andrew-clegg-linkedin | http://twitter.com/andrew_clegg

RE: replace value of a given field

Posted by Marek Miglinski <mm...@seven.com>.
Don't know if it's available through PIG, but I do it with custom Java UDF (ie Java's String.replace with params).



Sincerely,
Marek M.
________________________________________
From: Marco Cadetg [marco@zattoo.com]
Sent: Tuesday, October 11, 2011 10:51 AM
To: user@pig.apache.org
Subject: replace value of a given field

Hi there,

I would like to replace the value of a field based on its value. E.g.:

A = LOAD 'student' USING PigStorage() AS (name:chararray);
DUMP A;
(John)
(Mary
(Bill)
(Joe)
(John)

Now I would like to replace all John's with Marco. Is there a way to do this
in PIG? I thought about using sth like SPLIT IF John and then union the
result again. Is there a better way?

Cheers
-Marco