Support Questions

Find answers, ask questions, and share your expertise

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!