Michal
Michal

Reputation: 1895

concatenating text to a column in pig

I have a day column and a month column and would like to concatenate the year to it and store it in CHARARRAY format with the hyphens. so I have: month:CHARARRAY, day:CHARARRAY

Meaning, for example, if the day column contains '03' and the month column contains '04', I would like to create a date column that contains: '2014-04-03'

This is my code:

CONCAT('2014-',month,'-',day) as date;

It doesn't work and I'm not quite sure how to concatenate additional text onto the column. I would like to note that I'm not sure converting to date format is an option for me. I would prefer to keep it in CHARARRAY format since I would like to join with another file that has date stored in CHARARRAY format.

Upvotes: 1

Views: 6181

Answers (1)

Krati Jain
Krati Jain

Reputation: 368

Assuming this is the data file called dateExample.csv:

     Surender,02,03,1988
     Raja,12,09,1998
     Raj,05,10,1986

This is the script for pig:

   A = LOAD 'dateExample.csv' USING PigStorage(',') AS(name:chararray,day:chararray,month:long,year:chararray);
   X = FOREACH A GENERATE CONCAT((chararray)day,CONCAT('-',CONCAT((chararray)month,CONCAT('-',(chararray)year))));
   dump X;

You will get the desired output:

   (02-3-1988)
   (12-9-1998)
   (05-10-1986)

Explanation:

When we try to concat like this:

   X = FOREACH A GENERATE CONCAT(day,CONCAT('-',CONCAT(month,CONCAT('-',year))));

We get following exception :

  ERROR 1045: 
  <line 2, column 45> Could not infer the matching function for org.apache.pig.builtin.CONCAT as multiple or none of them fit. Please use an explicit cast.

So we need to explicitly cast the day,month and year values to chararray and it works!!

Upvotes: 4

Related Questions