PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFMinute.java

#
Java | 83 lines | 43 code | 10 blank | 30 comment | 2 complexity | 9deae61fb61d21300f04597264f0b8b8 MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.ql.udf;
20
21import java.text.ParseException;
22import java.text.SimpleDateFormat;
23import java.util.Calendar;
24import java.util.Date;
25
26import org.apache.hadoop.hive.ql.exec.Description;
27import org.apache.hadoop.hive.ql.exec.UDF;
28import org.apache.hadoop.io.IntWritable;
29import org.apache.hadoop.io.Text;
30
31/**
32 * UDFMinute.
33 *
34 */
35@Description(name = "minute",
36    value = "_FUNC_(date) - Returns the minute of date",
37    extended = "date is a string in the format of 'yyyy-MM-dd HH:mm:ss' or "
38    + "'HH:mm:ss'.\n"
39    + "Example:\n "
40    + "  > SELECT _FUNC_('2009-07-30 12:58:59') FROM src LIMIT 1;\n"
41    + "  58\n"
42    + "  > SELECT _FUNC_('12:58:59') FROM src LIMIT 1;\n" + "  58")
43public class UDFMinute extends UDF {
44  private final SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
45  private final SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
46  private final Calendar calendar = Calendar.getInstance();
47
48  private IntWritable result = new IntWritable();
49
50  public UDFMinute() {
51  }
52
53  /**
54   * Get the minute from a date string.
55   * 
56   * @param dateString
57   *          the dateString in the format of "yyyy-MM-dd HH:mm:ss" or
58   *          "yyyy-MM-dd".
59   * @return an int from 0 to 59. null if the dateString is not a valid date
60   *         string.
61   */
62  public IntWritable evaluate(Text dateString) {
63
64    if (dateString == null) {
65      return null;
66    }
67
68    try {
69      Date date = null;
70      try {
71        date = formatter1.parse(dateString.toString());
72      } catch (ParseException e) {
73        date = formatter2.parse(dateString.toString());
74      }
75      calendar.setTime(date);
76      result.set(calendar.get(Calendar.MINUTE));
77      return result;
78    } catch (ParseException e) {
79      return null;
80    }
81  }
82
83}