PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.udf;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import org.apache.hadoop.hive.ql.exec.Description;
  24. import org.apache.hadoop.hive.ql.exec.UDF;
  25. import org.apache.hadoop.io.IntWritable;
  26. import org.apache.hadoop.io.Text;
  27. /**
  28. * UDFMinute.
  29. *
  30. */
  31. @Description(name = "minute",
  32. value = "_FUNC_(date) - Returns the minute of date",
  33. extended = "date is a string in the format of 'yyyy-MM-dd HH:mm:ss' or "
  34. + "'HH:mm:ss'.\n"
  35. + "Example:\n "
  36. + " > SELECT _FUNC_('2009-07-30 12:58:59') FROM src LIMIT 1;\n"
  37. + " 58\n"
  38. + " > SELECT _FUNC_('12:58:59') FROM src LIMIT 1;\n" + " 58")
  39. public class UDFMinute extends UDF {
  40. private final SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  41. private final SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
  42. private final Calendar calendar = Calendar.getInstance();
  43. private IntWritable result = new IntWritable();
  44. public UDFMinute() {
  45. }
  46. /**
  47. * Get the minute from a date string.
  48. *
  49. * @param dateString
  50. * the dateString in the format of "yyyy-MM-dd HH:mm:ss" or
  51. * "yyyy-MM-dd".
  52. * @return an int from 0 to 59. null if the dateString is not a valid date
  53. * string.
  54. */
  55. public IntWritable evaluate(Text dateString) {
  56. if (dateString == null) {
  57. return null;
  58. }
  59. try {
  60. Date date = null;
  61. try {
  62. date = formatter1.parse(dateString.toString());
  63. } catch (ParseException e) {
  64. date = formatter2.parse(dateString.toString());
  65. }
  66. calendar.setTime(date);
  67. result.set(calendar.get(Calendar.MINUTE));
  68. return result;
  69. } catch (ParseException e) {
  70. return null;
  71. }
  72. }
  73. }