Ankush Rathi
Ankush Rathi

Reputation: 620

PIG - convert input date into UTC timezone

I have an input file with date in PST format

example => 2014-02-04 05:46:36.0

I need to have a pig syntax to convert this date into UTC.I tried using ToDate(input_date_column,'yyyy-MM-dd HH:mm:ss.SS','UTC') but it did not work.

Error shown - java.lang.IllegalArgumentException: Invalid format: ""2014-02-04 05:46:36.0""

Any help is appreciated :)

Upvotes: 0

Views: 474

Answers (1)

Ankush Rathi
Ankush Rathi

Reputation: 620

i could not really find some in build method for this one

so i wrote an user defined function and used it into my pig script

it goes like this -

public class convertToUTC extends EvalFunc<String> {
        @Override
        public String exec(final Tuple input) throws IOException {
            if (input == null || input.size() == 0) {
                return null;
            }
            try {
                String date = input.get(0).toString();
                Timestamp timestamp = Timestamp.valueOf(date);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(timestamp);
                calendar.add(Calendar.HOUR, 8);
                Timestamp UTCTimestamp = new Timestamp(calendar.getTime().getTime());
                return UTCTimestamp.toString();
            }
            catch (Exception e) {
                throw WrappedIOException.wrap("Caught exception processing input row ", e);
            }
        }
    }

Upvotes: 1

Related Questions