Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

How to get EPOCH time in PIG?

avatar
Expert Contributor

I am completely new to pig. Started to learn on my own and trying to know PIG. Can anyone please suggest me to get the current system epoch time using pig.

I need the output as

epochtime_1234567890

And how can we call the shell script in PIG script

Because I have already written to get EPOCH time in Shell script.

Any Help.

Thanks in Advance.

Mohan.v

1 ACCEPTED SOLUTION

avatar
Expert Contributor

Hey Mohan,

It might be cleaner to just write a UDF. I certainly feel like PIG has purposely left this functionality out to encourage the use of datetime over the use of epochs.

Here's one way to achieve what you want to do:

a = load 'test.data';
b = foreach a GENERATE *, CONCAT( 'epochtime_', (chararray)MilliSecondsBetween(CurrentTime(),ToDate(0)))
;dump b;

Hope this helps.

View solution in original post

4 REPLIES 4

avatar
Expert Contributor

Hey Mohan,

It might be cleaner to just write a UDF. I certainly feel like PIG has purposely left this functionality out to encourage the use of datetime over the use of epochs.

Here's one way to achieve what you want to do:

a = load 'test.data';
b = foreach a GENERATE *, CONCAT( 'epochtime_', (chararray)MilliSecondsBetween(CurrentTime(),ToDate(0)))
;dump b;

Hope this helps.

avatar
Expert Contributor
Thank for your reply Matt. I think i might got the solution
b =foreach a GENERATE CONCAT('epochtime_',(chararray) ToUnixTime(CurrentTime(0));
and i am able to get the output as i want.
but can you suggest me for,
reading a words.txt file having words and get the output as
word1_epochtime word2_epochtime.
Please don't hesitate to reply.

avatar
Expert Contributor

I think i got it too.

please correct me if im wrong

A = LOAD 'words.txt' AS (word:chararray);
B = FOREACH A GENERATE CONCAT(CONCAT(A.word,'_'),(chararray)ToUnixTime(CurrentTime());


dump B;

avatar
Expert Contributor

That Looks better than what I proposed. Thumbs up!