Pig - How to draw datetime for chararray

I am 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 tried various approaches, such as:

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

However, I always get ERROR 1052: cannot use datetime for chararray.

Does anyone know how I can do this? By the way, I am very new to pig. Thanks in advance!

+4
source share
2 answers

You need to create a custom UDF that performs the conversion (for example: see CurrentTime() implementation ). Alternatively, you can check my answer on a similar topic for workarounds.
If you are on AWS, use the DATE_TIME UDF.

+3
source

I had a similar problem and did not want to use custom UDF as described in another answer. I'm new to Pig, but it's a fairly simple operation that justifies the need for UDF. This command works fine for me:

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

You can select the desired format by looking at SimpleDateFormat javadoc

+6
source

Source: https://habr.com/ru/post/1483409/


All Articles