Pig - How to cast datetime to chararray
Asked Answered
O

2

5

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!

Orthodontist answered 29/5, 2013 at 16:37 Comment(0)
A
3

You need to create a custom UDF which does the conversion (e.g: see CurrentTime() implementation). Alternatively you may check out my answer on a similar topic for workarounds.
If you are on AWS, then use their DATE_TIME UDF.

Alexiaalexin answered 29/5, 2013 at 17:39 Comment(1)
I ended up using your workaround in your answer on the similar topic. It seems odd that there isn't an easier way. Thank you!Orthodontist
C
7

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

Caeoma answered 3/10, 2014 at 13:10 Comment(0)
A
3

You need to create a custom UDF which does the conversion (e.g: see CurrentTime() implementation). Alternatively you may check out my answer on a similar topic for workarounds.
If you are on AWS, then use their DATE_TIME UDF.

Alexiaalexin answered 29/5, 2013 at 17:39 Comment(1)
I ended up using your workaround in your answer on the similar topic. It seems odd that there isn't an easier way. Thank you!Orthodontist

© 2022 - 2024 — McMap. All rights reserved.