Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pig - How to cast datetime to chararray

Tags:

apache-pig

I'm using CurrentTime(), which is a datetime data type. However, I need it as a chararray. I have the following:

A = LOAD ...
B = FOREACH A GENERATE CurrentTime() AS todaysDate;

I've tried various approaches, such as the following:

B = FOREACH A GENERATE (chararray)CurrentTime() AS todaysDate;

However, I always get ERROR 1052: Cannot cast datetime to chararray.

Anyone know how I can do this? By the way, I'm very new to pig. Thanks in advance!

like image 961
MariaS Avatar asked Jan 13 '23 06:01

MariaS


1 Answers

I had a similar issue and I didn't want to use a custom UDF as described in the other answer. I am pretty new with Pig but it seems a pretty basic operation to justify the need of an UDF. This command works great for me:

B = FOREACH A GENERATE ToString(yourdatetimeobject, 'yyyy-MM-dd\'T\'HH:mm:ssz') AS yourfieldname;

You can select the format you want by looking at the SimpleDateFormat javadoc

like image 122
Avanbelle Avatar answered Feb 22 '23 21:02

Avanbelle