16 友盟项目--日期工具类、UTF自定义日期函数

日期工具类、UTF自定义日期函数

 
 
 
日期函数工具类
DateUtil
以date为参照,查询指定按照day、Month、week进行偏移日期时间格式
    /**
     * 以date为参照,查询指定按照day进行偏移日期时间格式
     */
    public static String formatDay(Date date , int offset , String fmt){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date) ;
        //在day的成分上进行累加累减
        cal.add(Calendar.DAY_OF_MONTH , offset);
        SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
        return sdf.format(cal.getTime()) ;
    }

    /**
     * 以date为参照,查询指定按照day进行偏移日期时间格式
     */
    public static String formatDay(long ts , int offset , String fmt){
        return formatDay(new Date(ts) , offset,fmt) ;
    }

    /**
     * 以date为参照,查询指定按照week进行偏移日期时间格式
     */
    public static String formatWeek(Date date , int offset , String fmt){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date) ;
        //取出指定date位于当前周的第几天
        int n = cal.get(Calendar.DAY_OF_WEEK) ;
        //在day的成分上进行累加累减
        cal.add(Calendar.DAY_OF_MONTH , -(n - 1));
        cal.add(Calendar.DAY_OF_MONTH , offset * 7);
        SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
        return sdf.format(cal.getTime()) ;
    }
    public static String formatWeek(long ts, int offset , String fmt){
        return formatWeek(new Date(ts) , offset , fmt) ;
    }
    /**
     * 以date为参照,查询指定按照month进行偏移日期时间格式
     */
    public static String formatMonth(Date date , int offset , String fmt){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date) ;
        cal.add(Calendar.MONTH , offset);
        SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
        return sdf.format(cal.getTime()) ;
    }
    public static String formatMonth(long ts, int offset , String fmt){
        return formatMonth(new Date(ts) , offset , fmt);
    }
}
DateUtil 日期工具类

自定义UTF日期工具类

按照day 进行偏移量计算

package com.oldboy.umeng.hive.udf;

import com.oldboy.umeng.hive.util.DateUtil;
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.ObjectInspectorConverters;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 按照天进行偏移量计算,格式化
 * formatday(-1)                取昨天的 yyyyMMdd
 * formatday(-1, 'yyyy/MM/dd')    取昨天的 yyyy/MM/dd
 * formatday(1548674636353 , -1 , 'yyyy/MM/dd')    取指定时间
 * formatday('2018-12-12' ,'yyyy-MM-dd' , -1 , 'yyyy/MM/dd') 取指定时间
 */
public class FormatByDayUDF extends GenericUDF {
    private ObjectInspectorConverters.Converter[] converters = new ObjectInspectorConverters.Converter[4] ;
    //对象检查器 初始化过程
    public ObjectInspector initialize(ObjectInspector[] ois) throws UDFArgumentException {
        int n = ois.length;
        if (ois.length < 1 || ois.length > 4) {
            throw new UDFArgumentException("参数个数不符!");
        }
        switch (n) {
            case 1:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{1}需要是int!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0] , PrimitiveObjectInspectorFactory.javaIntObjectInspector) ;
                break;
            case 2:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{1}需要是int!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{2}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
            case 3:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.LONG) {
                    throw new UDFArgumentException("参数{1}需要是bigint!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{2}需要是int!");
                }
                if (ois[2].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[2])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{3}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaLongObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[2] = ObjectInspectorConverters.getConverter(ois[2], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
            case 4:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{1}需要是string!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{2}需要是string!");
                }
                if (ois[2].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[2])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{3}需要是int!");
                }
                if (ois[3].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[3])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{4}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                converters[2] = ObjectInspectorConverters.getConverter(ois[2], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[3] = ObjectInspectorConverters.getConverter(ois[3], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
        }
        //返回字符串
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    /**
     * 实现计算过程
     */
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        //参数长度
        int n = arguments.length;
        if (arguments.length < 1 || arguments.length > 4) {
            throw new UDFArgumentException("参数个数不符!");
        }
        //根据参数个数 选择日期函数偏移量函数
        switch (n){
            case 1 :
            {
                //formatDay(-1)
                int offset = (Integer)converters[0].convert(arguments[0].get()) ;
                return DateUtil.formatDay(new Date() , offset , "yyyyMMdd") ;
            }
            case 2 :
            {
                //formatDay(-1 , 'yyyy/MM/dd')
                int offset = (Integer) converters[0].convert(arguments[0].get());
                String fmt= (String) converters[1].convert(arguments[1].get());
                return DateUtil.formatDay(new Date(), offset, fmt);
            }
            case 3 :
            {
                //formatDay(154xxxx ,,-1 , 'yyyy/MM/dd')
                long ts = (Long) converters[0].convert(arguments[0].get());
                int offset = (Integer) converters[1].convert(arguments[1].get());
                String fmt = (String) converters[2].convert(arguments[2].get());
                return DateUtil.formatDay(new Date(ts), offset, fmt);
            }
            case 4 :
            {
                try {
                    //formatDay('2018/12/12' , 'yyyy/MM/dd', -1 , 'yyyy/MM/dd')
                    String refDate = (String) converters[0].convert(arguments[0].get());
                    String refFmt = (String) converters[1].convert(arguments[1].get());
                    int offset = (Integer) converters[2].convert(arguments[2].get());
                    String fmt = (String) converters[3].convert(arguments[3].get());
                    SimpleDateFormat sdf = new SimpleDateFormat(refFmt) ;
                    return DateUtil.formatDay(sdf.parse(refDate), offset, fmt);
                } catch (Exception e) {
                    throw new UDFArgumentException("参数{2}日期格式不对!!!") ;
                }
            }
        }
        return null;
    }

    public String getDisplayString(String[] children) {
        return "formatbyday";
    }
}
FormatByDayUDF -按照天进行偏移量计算,格式化

按照month进行偏移量计算

package com.oldboy.umeng.hive.udf;

import com.oldboy.umeng.hive.util.DateUtil;
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.ObjectInspectorConverters;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 按照月进行偏移量计算,格式化
 * formatMonth(-1)                取昨天的 yyyyMM
 * formatMonth(-1, 'yyyy/MM')    取昨天的 yyyy/MM
 * formatMonth(1548674636353 , -1 , 'yyyy/MM/dd')    取指定时间
 * formatMonth('2018-12-12' ,'yyyy-MM-dd' , -1 , 'yyyy/MM/dd') 取指定时间
 */
public class FormatByMonthUDF extends GenericUDF {
    private ObjectInspectorConverters.Converter[] converters = new ObjectInspectorConverters.Converter[4] ;

    public ObjectInspector initialize(ObjectInspector[] ois) throws UDFArgumentException {
        int n = ois.length;
        if (ois.length < 1 || ois.length > 4) {
            throw new UDFArgumentException("参数个数不符!");
        }
        switch (n) {
            case 1:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{1}需要是int!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0] , PrimitiveObjectInspectorFactory.javaIntObjectInspector) ;
                break;
            case 2:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{1}需要是int!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{2}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
            case 3:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.LONG) {
                    throw new UDFArgumentException("参数{1}需要是bigint!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{2}需要是int!");
                }
                if (ois[2].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[2])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{3}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaLongObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[2] = ObjectInspectorConverters.getConverter(ois[2], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
            case 4:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{1}需要是string!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{2}需要是string!");
                }
                if (ois[2].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[2])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{3}需要是int!");
                }
                if (ois[3].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[3])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{4}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                converters[2] = ObjectInspectorConverters.getConverter(ois[2], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[3] = ObjectInspectorConverters.getConverter(ois[3], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
        }
        //返回字符串
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    /**
     * 实现计算过程
     */
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        int n = arguments.length;
        if (arguments.length < 1 || arguments.length > 4) {
            throw new UDFArgumentException("参数个数不符!");
        }
        switch (n){
            case 1 :
            {
                //formatMonth(-1)
                int offset = (Integer)converters[0].convert(arguments[0].get()) ;
                return DateUtil.formatMonth(new Date() , offset , "yyyyMM") ;
            }
            case 2 :
            {
                //formatMonth(-1 , 'yyyy/MM/dd')
                int offset = (Integer) converters[0].convert(arguments[0].get());
                String fmt= (String) converters[1].convert(arguments[1].get());
                return DateUtil.formatMonth(new Date(), offset, fmt);
            }
            case 3 :
            {
                //formatMonth(154xxxx ,,-1 , 'yyyy/MM/dd')
                long ts = (Long) converters[0].convert(arguments[0].get());
                int offset = (Integer) converters[1].convert(arguments[1].get());
                String fmt = (String) converters[2].convert(arguments[2].get());
                return DateUtil.formatMonth(new Date(ts), offset, fmt);
            }
            case 4 :
            {
                try {
                    //formatMonth('2018/12/12' , 'yyyy/MM/dd', -1 , 'yyyy/MM/dd')
                    String refDate = (String) converters[0].convert(arguments[0].get());
                    String refFmt = (String) converters[1].convert(arguments[1].get());
                    int offset = (Integer) converters[2].convert(arguments[2].get());
                    String fmt = (String) converters[3].convert(arguments[3].get());
                    SimpleDateFormat sdf = new SimpleDateFormat(refFmt) ;
                    return DateUtil.formatMonth(sdf.parse(refDate), offset, fmt);
                } catch (Exception e) {
                    throw new UDFArgumentException("参数{2}日期格式不对!!!") ;
                }
            }
        }
        return null;
    }

    public String getDisplayString(String[] children) {
        return "formatbymonth";
    }
}
FormatByMonthUDF -按照月进行偏移量计算,格式化

按照week 进行偏移量计算

package com.oldboy.umeng.hive.udf;

import com.oldboy.umeng.hive.util.DateUtil;
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.ObjectInspectorConverters;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 按照周进行偏移量计算,格式化
 * formatweek(-1)                取昨天的 yyyyMMdd
 * formatweek(-1, 'yyyy/MM/dd')    取昨天的 yyyy/MM/dd
 * formatweek(1548674636353 , -1 , 'yyyy/MM/dd')    取指定时间
 * formatweek('2018-12-12' ,'yyyy-MM-dd' , -1 , 'yyyy/MM/dd') 取指定时间
 */
public class FormatByWeekUDF extends GenericUDF {
    private ObjectInspectorConverters.Converter[] converters = new ObjectInspectorConverters.Converter[4] ;

    public ObjectInspector initialize(ObjectInspector[] ois) throws UDFArgumentException {
        int n = ois.length;
        if (ois.length < 1 || ois.length > 4) {
            throw new UDFArgumentException("参数个数不符!");
        }
        switch (n) {
            case 1:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{1}需要是int!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0] , PrimitiveObjectInspectorFactory.javaIntObjectInspector) ;
                break;
            case 2:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{1}需要是int!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{2}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
            case 3:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.LONG) {
                    throw new UDFArgumentException("参数{1}需要是bigint!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{2}需要是int!");
                }
                if (ois[2].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[2])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{3}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaLongObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[2] = ObjectInspectorConverters.getConverter(ois[2], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
            case 4:
                if (ois[0].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[0])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{1}需要是string!");
                }
                if (ois[1].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[1])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{2}需要是string!");
                }
                if (ois[2].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[2])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
                    throw new UDFArgumentException("参数{3}需要是int!");
                }
                if (ois[3].getCategory() != ObjectInspector.Category.PRIMITIVE
                            || (((PrimitiveObjectInspector) ois[3])).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) {
                    throw new UDFArgumentException("参数{4}需要是string!");
                }
                converters[0] = ObjectInspectorConverters.getConverter(ois[0], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                converters[1] = ObjectInspectorConverters.getConverter(ois[1], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                converters[2] = ObjectInspectorConverters.getConverter(ois[2], PrimitiveObjectInspectorFactory.javaIntObjectInspector);
                converters[3] = ObjectInspectorConverters.getConverter(ois[3], PrimitiveObjectInspectorFactory.javaStringObjectInspector);
                break;
        }
        //返回字符串
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    /**
     * 实现计算过程
     */
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        int n = arguments.length;
        if (arguments.length < 1 || arguments.length > 4) {
            throw new UDFArgumentException("参数个数不符!");
        }
        switch (n){
            case 1 :
            {
                //formatweek(-1)
                int offset = (Integer)converters[0].convert(arguments[0].get()) ;
                return DateUtil.formatWeek(new Date() , offset , "yyyyMMdd") ;
            }
            case 2 :
            {
                //formatweek(-1 , 'yyyy/MM/dd')
                int offset = (Integer) converters[0].convert(arguments[0].get());
                String fmt= (String) converters[1].convert(arguments[1].get());
                return DateUtil.formatWeek(new Date(), offset, fmt);
            }
            case 3 :
            {
                //formatweek(154xxxx ,,-1 , 'yyyy/MM/dd')
                long ts = (Long) converters[0].convert(arguments[0].get());
                int offset = (Integer) converters[1].convert(arguments[1].get());
                String fmt = (String) converters[2].convert(arguments[2].get());
                return DateUtil.formatWeek(new Date(ts), offset, fmt);
            }
            case 4 :
            {
                try {
                    //formatweek('2018/12/12' , 'yyyy/MM/dd', -1 , 'yyyy/MM/dd')
                    String refDate = (String) converters[0].convert(arguments[0].get());
                    String refFmt = (String) converters[1].convert(arguments[1].get());
                    int offset = (Integer) converters[2].convert(arguments[2].get());
                    String fmt = (String) converters[3].convert(arguments[3].get());
                    SimpleDateFormat sdf = new SimpleDateFormat(refFmt) ;
                    return DateUtil.formatWeek(sdf.parse(refDate), offset, fmt);
                } catch (Exception e) {
                    throw new UDFArgumentException("参数{2}日期格式不对!!!") ;
                }
            }
        }
        return null;
    }

    public String getDisplayString(String[] children) {
        return "formatbyweek";
    }
}
FormatByWeekUDF -按照周进行偏移量计算,格式化
spark-shell --jars /soft/hive/lib/umeng_hive.jar
 
2.注册函数脚本
 
use big12_umeng ;
drop function if exists forkstartuplogs ;
drop function if exists forkeventlogs ;
drop function if exists  forkerrorlogs ;
drop function if exists  forkpagelogs ;
drop function if exists  forkusagelogs ;
drop function if exists  formatbyday ;
create function forkstartuplogs as 'com.oldboy.umeng.hive.udtf.ForkStartuplogsUDTF' ;
create function forkeventlogs as 'com.oldboy.umeng.hive.udtf.ForkEventlogsUDTF' ;
create function forkerrorlogs as 'com.oldboy.umeng.hive.udtf.ForkErrorlogsUDTF' ;
create function forkpagelogs as 'com.oldboy.umeng.hive.udtf.ForkPagelogsUDTF' ;
create function forkusagelogs as 'com.oldboy.umeng.hive.udtf.ForkUsagelogsUDTF' ;
create function formatbyday as 'com.oldboy.umeng.hive.udf.FormatByDayUDF' ;
 
$scala>
val f = scala.io.Source.fromFile("/home/centos/big12_umeng/funcs.sql").mkString
 
val arr = f.split(";")
 
for (line <- arr){
  if(!line.equals("")){
    println(line) ;
    spark.sql(line).show()
  }
}
 

猜你喜欢

转载自www.cnblogs.com/star521/p/9912113.html