com.lithium.yoda.ToEpoch.java Source code

Java tutorial

Introduction

Here is the source code for com.lithium.yoda.ToEpoch.java

Source

/*
 * Licensed to Lithium Technologies Inc. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lithium.yoda;

import com.google.common.base.Joiner;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantStringObjectInspector;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.sql.Timestamp;

/**
 * @author pradeep.gollakota
 */
public class ToEpoch extends GenericUDF {

    private ObjectInspector dateOi;
    private StringObjectInspector formatOi;
    private String constSecondArg;
    private boolean hasSecondArg;

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        dateOi = arguments[0];
        if (!(dateOi instanceof StringObjectInspector || dateOi instanceof TimestampObjectInspector)) {
            throw new IllegalArgumentException(
                    "Can only call this UDF on types 'string' and 'timestamp'. " + "Given " + dateOi.getTypeName());
        }

        hasSecondArg = arguments.length > 1;

        if (hasSecondArg) {
            ObjectInspector oi = arguments[1];
            if (oi instanceof WritableConstantStringObjectInspector) {
                constSecondArg = ((WritableConstantStringObjectInspector) oi).getWritableConstantValue().toString();
            } else if (oi instanceof StringObjectInspector) {
                formatOi = (StringObjectInspector) oi;
            } else {
                throw new IllegalArgumentException("Second argument must be of String type");
            }
        }
        return PrimitiveObjectInspectorFactory.javaLongObjectInspector;
    }

    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        if (dateOi instanceof StringObjectInspector) {
            String dateStr = ((StringObjectInspector) dateOi).getPrimitiveJavaObject(arguments[0].get());
            DateTimeFormatter dtf;
            if (hasSecondArg) {
                if (constSecondArg == null) {
                    dtf = DateTimeFormat.forPattern(formatOi.getPrimitiveJavaObject(arguments[1].get()));
                } else {
                    dtf = DateTimeFormat.forPattern(constSecondArg);
                }
            } else {
                dtf = ISODateTimeFormat.dateTimeParser().withOffsetParsed();
            }
            return dtf.parseDateTime(dateStr).getMillis();
        } else if (dateOi instanceof TimestampObjectInspector) {
            Timestamp ts = ((TimestampObjectInspector) dateOi).getPrimitiveJavaObject(arguments[0].get());
            if (hasSecondArg) {
                DateTime dt;
                if (constSecondArg == null) {
                    dt = new DateTime(ts.getTime(),
                            DateTimeZone.forID(formatOi.getPrimitiveJavaObject(arguments[1].get())));
                } else {
                    dt = new DateTime(ts.getTime(), DateTimeZone.forID(constSecondArg));
                }
                return dt.getMillis();
            } else {
                return ts.getTime();
            }
        }

        return null;
    }

    @Override
    public String getDisplayString(String[] children) {
        StringBuilder sb = new StringBuilder(32);
        sb.append("to_epoch(");
        Joiner.on(',').appendTo(sb, children);
        sb.append(')');
        return sb.toString();
    }
}