当前位置:   article > 正文

Hive函数 date_format 使用示例总结_hive dateformat

hive dateformat

Hive函数 date_format 使用示例总结

Hive函数 date_format 用于将日期或时间戳格式化为指定的输出格式。假设要对时间 2024-03-18 18:18:18.008 进行格式化,以下是一些常见的时间提取格式,这些格式可以在 date_format 函数中使用:

1. yyyy :四位年份,如2024。
SELECT date_format('2024-03-18 18:18:18.008', 'yyyy');
输出:
2024
  • 1
  • 2
  • 3
2. yy :两位年份,如24。
SELECT date_format('2024-03-18 18:18:18.008', 'yy');
输出:
24
  • 1
  • 2
  • 3
3. MM :两位月份,如01表示一月。
SELECT date_format('2024-03-18 18:18:18.008', 'MM');
输出:
03
  • 1
  • 2
  • 3
4. M :一位或两位月份,如1表示一月。
SELECT date_format('2024-03-18 18:18:18.008', 'M');
输出:
3
  • 1
  • 2
  • 3
5. dd :两位日期,如01表示第一天。
SELECT date_format('2024-03-18 18:18:18.008', 'dd');
输出:
18
  • 1
  • 2
  • 3
6. d :一位或两位日期,如1表示第一天。
SELECT date_format('2024-03-18 18:18:18.008', 'd');
输出:
18
  • 1
  • 2
  • 3
7. HH :24小时制的小时,如00表示午夜。
SELECT date_format('2024-03-18 18:18:18.008', 'HH');
输出:
18
  • 1
  • 2
  • 3
8. hh :12小时制的小时,如12表示中午或午夜。
SELECT date_format('2024-03-18 18:18:18.008', 'hh');
输出:
6
  • 1
  • 2
  • 3
9. mm :分钟,如00表示整点。
SELECT date_format('2024-03-18 18:18:18.008', 'mm');
输出:
18
  • 1
  • 2
  • 3
10. ss :秒,如00表示整分。
SELECT date_format('2024-03-18 18:18:18.008', 'ss');
输出:
18
  • 1
  • 2
  • 3
11. S :毫秒,如000表示整毫秒。
SELECT date_format('2024-03-18 18:18:18.008', 'S');
输出:
8
  • 1
  • 2
  • 3
12. a :上午或下午。
SELECT date_format('2024-03-18 18:18:18.008', 'a');
输出:
下午
  • 1
  • 2
  • 3
13. E :星期几的全名,如星期一。
SELECT date_format('2024-03-18 18:18:18.008', 'E');
输出:
星期一
  • 1
  • 2
  • 3
14. w :一年中的第几周。
SELECT date_format('2024-03-18 18:18:18.008', 'w');
输出:
12
  • 1
  • 2
  • 3
15. W :一个月中的第几周。
SELECT date_format('2024-03-18 18:18:18.008', 'W');
输出:
4
  • 1
  • 2
  • 3
16. D :一年中的第几天。
SELECT date_format('2024-03-18 18:18:18.008', 'D');
输出:
78
  • 1
  • 2
  • 3
17. F :一个月中的第三个星期一。
SELECT date_format('2024-03-18 18:18:18.008', 'F');
输出:
3
  • 1
  • 2
  • 3
18. u :ISO-8601标准的星期几,1表示星期一。
SELECT date_format('2024-03-18 18:18:18.008', 'u');
输出:
1
  • 1
  • 2
  • 3

以上SQL示例是根据不同的时间提取格式,对给定的时间 2024-03-18 18:18:18.008 进行格式化处理。


Hive 函数 date_format 部分源码如下:

  @Override
  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
	...
        try {
          formatter = new SimpleDateFormat(fmtStr);
          formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        } catch (IllegalArgumentException e) {
          // ignore
        }
    ...
   }

  @Override
  public Object evaluate(DeferredObject[] arguments) throws HiveException {
  	...
    String res = formatter.format(date);
    if (res == null) {
      return null;
    }
    output.set(res);
    return output;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

通过查询date_format源码得知,在初始化阶段,函数会尝试使用给定的格式字符串( fmtStr )创建一个 SimpleDateFormat 对象,并将其时区设置为UTC。在 evaluate 方法中,函数通过 SimpleDateFormat 对象对输入的日期进行格式化处理,生成格式化后的字符串。如果格式化后的结果为null,则返回null,否则将结果设置到输出对象中并返回。

总之,Hive 函数 date_format 能够支持的时间格式化取决于 SimpleDateFormat 这个类能够支持的类型。我们可以通过查阅 SimpleDateFormat 类的官方文档来获取更详细的信息:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号