1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * Licensed to the Apache Software Foundation (ASF) 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 org.apache.hadoop.hive.ql.udf;

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

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;

/**
 * UDFUnixTimeStamp.
 *
 */
@UDFType(deterministic = false)
@Description(name = "unix_timestamp",
    value = "_FUNC_([date[, pattern]]) - Returns the UNIX timestamp",
    extended = "Converts the current or specified time to number of seconds "
    + "since 1970-01-01.")
public class UDFUnixTimeStamp extends UDF {
  // For now, we just use the default time zone.
  private final SimpleDateFormat formatter = new SimpleDateFormat(
      "yyyy-MM-dd HH:mm:ss");

  LongWritable result = new LongWritable();

  public UDFUnixTimeStamp() {
  }

  /**
   * Return current UnixTime.
   * 
   * @return long Number of seconds from 1970-01-01 00:00:00
   */
  public LongWritable evaluate() {
    Date date = new Date();
    result.set(date.getTime() / 1000);
    return result;
  }

  /**
   * Convert time string to UnixTime.
   * 
   * @param dateText
   *          Time string in format yyyy-MM-dd HH:mm:ss
   * @return long Number of seconds from 1970-01-01 00:00:00
   */
  public LongWritable evaluate(Text dateText) {
    if (dateText == null) {
      return null;
    }

    try {
      Date date = formatter.parse(dateText.toString());
      result.set(date.getTime() / 1000);
      return result;
    } catch (ParseException e) {
      return null;
    }
  }

  Text lastPatternText = new Text();

  /**
   * Convert time string to UnixTime with user defined pattern.
   * 
   * @param dateText
   *          Time string in format patternstring
   * @param patternText
   *          Time patterns string supported by SimpleDateFormat
   * @return long Number of seconds from 1970-01-01 00:00:00
   */
  public LongWritable evaluate(Text dateText, Text patternText) {
    if (dateText == null || patternText == null) {
      return null;
    }
    try {
      if (!patternText.equals(lastPatternText)) {
        formatter.applyPattern(patternText.toString());
        lastPatternText.set(patternText);
      }
    } catch (Exception e) {
      return null;
    }

    return evaluate(dateText);
  }
}
			
			

Browsed Source: [clear]