hive 时间转字符串_hive日期函数

2023-05-16

hive的函数有很多,今天我带大家来总结下hive中常用的日期函数吧!!!

如有不足,还请大家多多指出,希望能和大家一起交流,共同进步!

03b43daac44978b21bdfe6a2f31e1e92.png

时间一点一滴在溜走,我们要珍惜每一刻

1.日期时间转日期函数:to_date()

to_date(string timestamp) 返回日期时间字段中的日期部分。

返回类型:string

select to_date('2011-12-08 10:03:01') --2011-12-08

2.获取当前日期:current_date()

current_date() 返回当前时间日期

返回类型:date

select current_date() --2019-02-14

3.查询当前系统时间(包括毫秒数): current_timestamp()

current_timestamp() 返回当前时间戳

返回类型:timestamp

select current_timestamp() --2019-02-14 18:50:50.241

4.日期增加函数:date_add()

date_add(string startdate, int days) 返回开始日期startdate增加days天后的日期

返回类型:string

select date_add('2018-12-31', 1) --2019-01-01

5.日期减少函数:date_sub()

date_sub (string startdate, int days) 返回开始日期startdate减少days天后的日期

返回类型:string

select date_sub('2018-12-31', 1) --2018-12-30

6.日期比较函数:datediff()

datediff(string enddate, string startdate) 返回结束日期减去开始日期的天数

返回类型:int

select datediff('2019-02-15','2019-02-01') --14

7.日期格式化,按照格式返回字符串:date_format()

date_format(date/timestamp/string, string fmt) 按指定格式返回date

返回类型: string

select date_format('2019-04-08 12:12:12','yyyy-MM-dd') --2019-04-08select date_format('2019-04-08 12:12:12','yyyyMMdd') --20190408select date_format('2019-04-08 12:12:12','yyyy-MM') --2019-04select date_format('2019-04-08 12:12:12','yyyy') --2019

8.日期转年函数:year()

year(string/date) 返回时间字符串的年份部分

返回类型:int

select year('2019-04-08 12:12:12') --2019

9.月份函数:month()

month(string/date) 返回时间字符串的月份

返回类型:int

select month('2019-04-11') --4

10.天函数:day() /dayofmonth(date)

day(string/date) 返回时间字符串的天

返回类型:int

select day('2019-04-08 12:12:12') --8select day('2019-04-11') --11select dayofmonth('2019-04-08 12:13:11') --8

11.小时函数:hour()

hour(string/date) 返回时间字符串的小时数字

返回类型:int

select hour('2019-04-08 12:13:11') --12

12.分钟函数:minute()

minute(string/date) 返回时间字符串的分钟数字

返回类型:int

select minute('2019-04-08 12:13:11') --13

13.秒函数:second()

second(string/date) 返回时间字符串的分钟数字

返回类型:int

select second('2019-04-08 12:13:11') --11

14.月份差:months_between()

months_between(date1, date2) 返回date1与date2之间相差的月份,如date1>date2,则返回正,如果date1

返回类型:double

select months_between('2019-02-25','2019-01-26') --0.96774194select months_between('2019-02-25','2019-02-26') --0.03225806select months_between('2019-02-25','2019-02-25') --0

15.增加月份:add_months()

add_months(string start_date, int num_months) 返回当前时间下再增加num_months个月的日期

返回类型:string

select add_months('2019-01-01',2) --2019-03-01

16.查询时间字符串位于一年中的第几个周内:weekofyear()

weekofyear(string/date) 返回时间字符串位于一年中的第几个周内

返回类型:int

select weekofyear('2019-04-08 12:13:11') --15

17.查询当月第几天: dayofmonth(current_date)

返回类型:int

select dayofmonth(current_date()) --14

18.返回月末: last_day()

last_day(string date) 返回这个月的最后一天的日期,忽略时分秒部分(HH:mm:ss)

返回类型:string

select last_day(current_date()) --2019-02-28

19.返回时间的最开始年份或月份 :trunc()

trunc(string date, string format) 返回时间的最开始年份或月份

返回类型:string

select trunc('2019-04-08','YY') --2019-01-01select trunc('2019-04-08','MM') --2019-04-01
abe419f896b216812aebb54143e9b4b6.png

诗和远方

20.返回当月第1天:

有多种方法可以灵活使用,这里我列出我经常使用的两种方法

1).trunc(current_timestamp(),'MM')

select trunc(current_timestamp(),'MM') ----2019-02-01

2).date_sub(current_date,dayofmonth(current_date)-1)

select date_sub(current_date,dayofmonth(current_date)-1) --2019-02-01

21.返回下个月第1天:

1).trunc(add_months(current_timestamp(),1),'MM')

select trunc(add_months(current_timestamp(),1),'MM') --2019-03-01

2). add_months(date_sub(current_date,dayofmonth(current_date)-1),1)

select add_months(date_sub(current_date,dayofmonth(current_date)-1),1) --2019-03-01

22.返回上个月第一天

1). trunc(add_months(current_timestamp(),-1),'MM')

select trunc(add_months(current_timestamp(),-1),'MM') --2019-01-01

2). add_months(date_sub(current_date,dayofmonth(current_date)-1),-1)

select add_months(date_sub(current_date,dayofmonth(current_date)-1),-1) --2019-01-01

23. 下周几的具体日期: next_day()

next_day(string date, string week) 返回当前时间的下一个星期X所对应的日期

返回类型:string

下周一:select next_day(to_date(CURRENT_TIMESTAMP),'MO') --2019-02-18本周一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),7) --2019-02-11上周一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),14) --2019-02-04
430918d65b021f6f9da6b430037c9a22.png

工作之余,小酌一杯吧

24.UNIX时间戳转日期函数:from_unixtime()

from_unixtime(bigint unixtime[, string format]) 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

返回类型:string

select from_unixtime(1323308143,'yyyy-MM-dd') --2011-12-08

25.获取当前UNIX时间戳函数: unix_timestamp()

返回类型: bigint

select unix_timestamp() --1554721853

26.日期转UNIX时间戳函数: unix_timestamp()

unix_timestamp(string date) 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0

返回类型: bigint

 select unix_timestamp('2019-03-07 13:01:03') --1551934863

27.指定格式日期转UNIX时间戳函数: unix_timestamp

unix_timestamp(string date, string pattern) 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0

返回类型: bigint

unix_timestamp('2009-03-20', 'yyyy-MM-dd') --1553011200

28.hive中获取当前时间

from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hive 时间转字符串_hive日期函数 的相关文章

  • Hive 函数替换列值中的逗号

    我有一个配置单元表 其中字符串列的值为 12 345 有没有什么方法可以在插入此配置单元表期间删除逗号的配置单元函数 您可以使用regexp replace string INITIAL STRING string PATTERN stri
  • Apache Hive - 复杂数据类型映射 不起作用

    蜂巢版本2 1 1 问题描述 集合项终止值作为映射键插入 蜂巢表 CREATE TABLE profiles id int name struct
  • 在 HIVE 中查找函数

    我想检查一个字段是否包含字符串 我想要一个如下所示的函数 FIND string to find field to search 我的数据如下所示 field to search no match in this string record
  • HIVE JDBC ThriftHive$Client.sendBase

    我在 Hadoop hive 上工作 我已经安装了 hadoop 和 hive 它在命令提示符下运行良好 我还创建了 hive 的 MySQL 元存储 我在 hive site xml 文件中定义了 HIVE DB 数据库名称 MySQL
  • Presto/Athena 中嵌套日期分区的比较查询

    我将 parquet 数据存储在 S3 上 以 Hive 理解的格式进行分区 s3
  • 如何在 Hive 中将字符串转换为毫秒时间戳

    我有一个字符串 20141014123456789 它代表一个毫秒时间戳 我需要将其转换为 Hive 中的时间戳 0 13 0 而不丢失毫秒 我尝试了这个 但 unix timestamp 返回一个整数 所以我丢失了毫秒 from unix
  • Hive:转换“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”中缺少秒数的字符串日期时间

    我使用以下代码将字符串日期时间变量转换为日期时间 但转换后的字符串缺少 SSS 部分 使用的代码 cast FROM UNIXTIME UNIX TIMESTAMP oldtime yyyy MM dd T HH mm ss SSS Z y
  • Spark SQL 未正确转换时区[重复]

    这个问题在这里已经有答案了 使用 Scala 2 10 4 和 Spark 1 5 1 和 Spark 1 6 sqlContext sql select id to date from utc timestamp from unixtim
  • HIVE:GROUP BY 的行为与 MySQL 中不同

    我对 MySQL 有一些经验 最近我必须在 HIVE 上做一些工作 两者之间的查询基本结构非常相似 但是 HIVE 中的 GROUP BY 的工作方式似乎有点不同 因此我无法实现以前在 MySQL 中使用 GROUP BY 可以实现的目标
  • Hive 表的默认分隔符是什么?

    如果我们在创建表时不提及任何分隔符 hive 是否有默认分隔符 创建表日志 ts bigint 行字符串 按 dt 字符串 国家 地区字符串 分区 默认分隔符 001 如果创建hive表时没有设置 您可以将其更改为其他分隔符 例如 hive
  • Hive(查找连续 n 列中的最小值)

    我在 Hive 中有一个表 有 5 列 即电子邮件 a first date b first date c first date d first date a b c d 是用户可以执行的 4 个不同操作 上表中的 4 列表示用户执行第一个
  • Hive 中 Sortby 和 orderby 查询的区别

    Hive sort by and order by命令用于按排序顺序获取数据 例如 Sort by hive gt SELECT E EMP ID FROM Employee E SORT BY E empid Order by hive
  • Hive查询快速查找表大小(行数)

    是否有 Hive 查询可以快速查找表大小 即行数 而无需启动耗时的 MapReduce 作业 这就是为什么我想避免COUNT I tried DESCRIBE EXTENDED 但这产生了numRows 0这显然是不正确的 对新手问题表示歉
  • Spark 上的 Hive 2.1.1 - 我应该使用哪个版本的 Spark

    我在跑蜂巢2 1 1 Ubuntu 16 04 上的 hadoop 2 7 3 根据Hive on Spark 入门 https cwiki apache org confluence display Hive Hive on Spark
  • 如何将Hive数据表迁移到MySql?

    我想知道如何将日期从 Hive 转移到 MySQL 我看过有关如何将 Hive 数据移动到 Amazon DynamoDB 的示例 但没有看到有关如何将 Hive 数据移动到 MySQL 等 RDBMS 的示例 这是我在 DynamoDB
  • Hive - 线程安全的自动递增序列号生成

    我遇到一种情况 需要将记录插入到特定的 Hive 表中 其中一列需要是自动递增的序列号 即在任何时间点都必须严格遵循 max value 1 规则 记录从许多并行的 Hive 作业插入到这个特定的表中 这些作业每天 每周 每月批量运行 现在
  • Hive“添加分区”并发

    我们有一个外部 Hive 表 用于处理原始日志文件数据 这些文件每小时一次 并按日期和源主机名分区 目前 我们正在使用简单的 python 脚本导入文件 这些脚本每小时触发几次 该脚本根据需要在 HDFS 上创建子文件夹 从临时本地存储复制
  • Spark SQL sql("").first().getDouble(0) 给我不一致的结果

    我有下面的查询 它应该找到列值的平均值并返回一个数字的结果 val avgVal hiveContext sql select round avg amount 4 from users payment where dt between 2
  • hive 添加分区语句忽略前导零

    我在 hdfs 上有文件夹 user test year 2016 month 04 dt 25 000000 0 需要将上面的分区路径添加到test table 命令 ALTER TABLE test ADD IF NOT EXISTS
  • 无法验证 serde:org.openx.data.jsonserde.jsonserde

    我编写了这个查询来在配置单元上创建一个表 我的数据最初是 json 格式 所以我已经下载并构建了 serde 并添加了它运行所需的所有 jar 但我收到以下错误 FAILED Execution Error return code 1 fr

随机推荐