irpas技术客

Hive时间(日期)函数_王小磊~_hive的时间函数

irpas 5108

Hive内置函数地址

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

日期函数

Hive 中支持以下内置日期函数:

返回类型名称描述字符串from_unixtime(bigint unixtime[, string format])将 unix 纪元 (1970-01-01 00:00:00 UTC) 中的秒数转换为表示当前系统时区中该时刻的时间戳的字符串(使用配置"hive.local.time.zone"),格式为"uuuu-MM-dd HH:mm:ss" 示例"1970-01-01 00:00:00"。在 Hive 4.0.0 (HIVE-25458) 之前,它使用 [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html],因此支持的格式已更改。bigintunix_timestamp()获取当前 Unix 时间戳(以秒为单位)。此函数不是确定性的,并且其值对于查询执行的范围不是固定的,因此会阻止对查询进行适当的优化 - 自 2.0 以来已弃用,取而代之的是CURRENT_TIMESTAMP常量。bigintunix_timestamp(字符串日期)使用默认时区和默认区域设置(即(使用配置"hive.local.time.zone")通过 [https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html] 将格式的时间字符串转换为 Unix 时间戳(以秒为单位),如果失败,则返回 0:unix_timestamp(‘2009-03-20 11:30:01’) = 1237573801uuuu-MM-dd HH:mm:ss在 Hive 4.0.0 (HIVE-25458) 之前,它使用 [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html],因此引用的字符串格式为yyyy-MM-dd HH:mm:ss.bigintunix_timestamp(字符串日期、字符串模式)将具有给定模式的时间字符串(参见 [https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html])转换为 Unix 时间戳(以秒为单位),如果失败,则返回 0:unix_timestamp(‘2009-03-20’, ‘uuuu-MM-dd’) = 1237532400。在 Hive 4.0.0 (HIVE-25458) 之前,它使用 [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html],因此支持的模式已更改。2.1.0 之前:字符串2.1.0 后: 日期to_date(字符串时间戳)返回时间戳字符串(Hive 2.1.0 之前)的日期部分:to_date(“1970-01-01 00:00:00”) = “1970-01-01”。从 Hive 2.1.0 开始,返回一个日期对象。在 Hive 2.1.0 (HIVE-13248) 之前,返回类型为 String,因为在创建该方法时不存在 Date 类型。intyear(string date)返回日期或时间戳字符串的年份部分:year(“1970-01-01 00:00:00”) = 1970,year(“1970-01-01”) = 1970。intquarter(date/timestamp/string)返回日期、时间戳或字符串的一年中季度,范围为 1 到 4(从 Hive 1.3.0 开始)。示例:季度(‘2015-04-08’) = 2。intmonth(string date)返回日期或时间戳字符串的月份部分:month(“1970-11-01 00:00:00”) = 11, month(“1970-11-01”) = 11。intday(string date) dayofmonth(date)返回日期或时间戳字符串的 day 部分:day(“1970-11-01 00:00:00”) = 1,day(“1970-11-01”) = 1。inthour(string date)返回时间戳的小时:小时(‘2009-07-30 12:58:59’) = 12,小时(‘12:58:59’) = 12。intminute(string date)返回时间戳的分钟数。intsecond(string date)返回时间戳的第二个。intweekofyear(string date)返回时间戳字符串的周数:weekofyear(“1970-11-01 00:00:00”) = 44,weekofyear(“1970-11-01”) = 44。intextract(field FROM source)从源中检索天数或小时等字段(从 Hive 2.2.0 开始)。源必须是日期、时间戳、间隔或可转换为日期或时间戳的字符串。支持的字段包括:天、星期、小时、分钟、月、季度、秒、周和年。例子:选择摘录(月份从"2016-10-20")结果为10。选择摘录(小时从"2016-10-20 05:06:07")结果5。选择摘录(星期二从"2016-10-20 05:06:07")结果5。选择提取(从间隔"1-3"年到月份的月份)结果为3。选择提取(从间隔"3 12:20:30"到秒的分钟)结果为20。intdatediff(string enddate, string startdate)返回从开始日期到结束日期的天数:datediff(‘2009-03-01’, ‘2009-02-27’) = 2。2.1.0 之前:字符串2.1.0 在: 日期date_add(日期/时间戳/字符串开始日期,tinyint/smallint/int days)将开始日期的天数相加:date_add(‘2008-12-31’, 1) = ‘2009-01-01’。在 Hive 2.1.0 (HIVE-13248) 之前,返回类型为 String,因为在创建该方法时不存在 Date 类型。2.1.0 之前:字符串2.1.0 在: 日期date_sub(日期/时间戳/字符串开始日期,tinyint/smallint/int days)减去开始日期的天数:date_sub(‘2008-12-31’, 1) = ‘2008-12-30’。在 Hive 2.1.0 (HIVE-13248) 之前,返回类型为 String,因为在创建该方法时不存在 Date 类型。时间戳from_utc_timestamp({任何基元类型} ts,字符串时区)将 UTC 格式的时间戳* 转换为给定时区(从 Hive 0.8.0 开始)。* 时间戳是一种基元类型,包括时间戳/日期、tinyint/smallint/int/bigint、float/double 和 decimal。 小数值被视为秒。整数值被视为毫秒。例如,from_utc_timestamp(2592000.0,‘PST’)、from_utc_timestamp(2592000000,‘PST’) 和 from_utc_timestamp(时间戳 ‘1970-01-30 16:00:00’,‘PST’)都返回时间戳 1970-01-30 08:00:00。时间戳to_utc_timestamp({任何**基元类型} ts,字符串时区)将给定时区中的时间戳* 转换为 UTC(从 Hive 0.8.0 开始)。* 时间戳是一种基元类型,包括时间戳/日期、tinyint/smallint/int/bigint、float/double 和 decimal。小数值被视为秒。整数值被视为毫秒。例如,to_utc_timestamp(2592000.0,‘PST’)、to_utc_timestamp(2592000000,‘PST’) 和 to_utc_timestamp(时间戳 ‘1970-01-30 16:00:00’,‘PST’)都返回时间戳 1970-01-31 00:00:00。日期current_date返回查询计算开始时的当前日期(从 Hive 1.2.0 开始)。同一查询中对current_date的所有调用都返回相同的值。时间戳current_timestamp返回查询评估开始时的当前时间戳(从 Hive 1.2.0 开始)。同一查询中对current_timestamp的所有调用都返回相同的值。字符串add_months(字符串start_date、整数num_months、output_date_format)返回在start_date之后num_months的日期(从 Hive 1.1.0 开始)。start_date是字符串、日期或时间戳。num_months是一个整数。 如果start_date是该月的最后一天,或者如果生成的月份比start_date的天部分的天数少,则结果是结果为结果月份的最后一天。否则,结果与start_date具有同一天分量。默认输出格式为"yyyy-MM-dd"。在 Hive 4.0.0 之前,日期的时间部分将被忽略。从 Hive 4.0.0 开始,add_months 支持可选参数 output_date_format,该参数接受表示输出的有效日期格式的字符串。这允许在输出中保留时间格式。例如:add_months(‘2009-08-31’, 1) 返回 ‘2009-09-30’。 add_months(‘2017-12-31 14:15:16’, 2, ‘YYYY-MM-dd HH:mm:ss’) 返回 ‘2018-02-28 14:15:16’。字符串last_day(字符串日期)返回该日期所属的月份的最后一天(从 Hive 1.1.0 开始)。date 是格式为"yyyy-MM-dd HH:mm:ss"或"yyyy-MM-dd"的字符串。日期的时间部分将被忽略。字符串next_day(字符串start_date、字符串day_of_week)返回晚于 start_date 并命名为 day_of_week(自 Hive 1.2.0 起)的第一个日期。start_date是字符串/日期/时间戳。day_of_week为 2 个字母、3 个字母或星期几的全名(例如,星期二、星期五)。start_date的时间部分将被忽略。示例:next_day(‘2015-01-14’, ‘TU’) = 2015-01-20。字符串截断(字符串日期,字符串格式)返回截断到格式指定的单位的日期(从 Hive 1.2.0 开始)。支持的格式:月/月/月、年/年/年。示例:trunc(‘2015-03-17’, ‘MM’) = 2015-03-01。doublemonths_between(日期1,日期2)返回日期 date1 和 date2 之间的月数(自 Hive 1.2.0 起)。如果 date1 晚于 date2,则结果为正数。如果 date1 早于 date2,则结果为阴性。如果 date1 和 date2 是该月的同一天或月份的最后两天,则结果始终为整数。否则,UDF 将根据 31 天的月份计算结果的小数部分,并考虑时间分量 date1 和 date2 的差异。date1 和 date2 类型可以是日期、时间戳或字符串,格式为"yyyy-MM-dd"或"yyyy-MM-dd HH:mm:ss"。结果将四舍五入到小数点后 8 位。示例:months_between(‘1997-02-28 10:30:00’, ‘1996-10-30’) = 3.94959677字符串date_format(日期/时间戳/字符串 ts、字符串 fmt)将日期/时间戳/字符串转换为日期格式 fmt(从 Hive 1.2.0 开始)指定的格式的字符串值。支持的格式是 Java DateTimeFormatter 格式 – https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html。第二个参数 fmt 应该是常量。示例:date_format(‘2015-04-08’, ‘y’) = ‘2015’。date_format可用于实现其他 UDF,例如:日名(日期)date_format(日期,‘EEEE’)年(日期)为date_format(日期,“D”)在 Hive 4.0.0 (HIVE-25458) 之前,它使用 [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html],因此支持的模式已更改。
Date Functions

The following built-in date functions are supported in Hive:

Return TypeName(Signature)Descriptionstringfrom_unixtime(bigint unixtime[, string format])Converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone(using config “hive.local.time.zone”) in the format of “uuuu-MM-dd HH:mm:ss” example"1970-01-01 00:00:00".Prior to Hive 4.0.0 (HIVE-25458), it uses [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html] and hence the supported format have changed.bigintunix_timestamp()Gets current Unix timestamp in seconds. This function is not deterministic and its value is not fixed for the scope of a query execution, therefore prevents proper optimization of queries - this has been deprecated since 2.0 in favour of CURRENT_TIMESTAMP constant.bigintunix_timestamp(string date)Converts time string in format uuuu-MM-dd HH:mm:ss to Unix timestamp (in seconds) via [https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html], using the default timezone and the default locale i.e (using config “hive.local.time.zone”), return 0 if fail: unix_timestamp(‘2009-03-20 11:30:01’) = 1237573801Prior to Hive 4.0.0 (HIVE-25458), it uses [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html] and hence the string format referred was yyyy-MM-dd HH:mm:ss.bigintunix_timestamp(string date, string pattern)Convert time string with given pattern (see [https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html]) to Unix time stamp (in seconds), return 0 if fail: unix_timestamp(‘2009-03-20’, ‘uuuu-MM-dd’) = 1237532400.Prior to Hive 4.0.0 (HIVE-25458), it uses [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html] and hence the supported patterns have changed.pre 2.1.0: string2.1.0 on: dateto_date(string timestamp)Returns the date part of a timestamp string (pre-Hive 2.1.0): to_date(“1970-01-01 00:00:00”) = “1970-01-01”. As of Hive 2.1.0, returns a date object.Prior to Hive 2.1.0 (HIVE-13248) the return type was a String because no Date type existed when the method was created.intyear(string date)Returns the year part of a date or a timestamp string: year(“1970-01-01 00:00:00”) = 1970, year(“1970-01-01”) = 1970.intquarter(date/timestamp/string)Returns the quarter of the year for a date, timestamp, or string in the range 1 to 4 (as of Hive 1.3.0). Example: quarter(‘2015-04-08’) = 2.intmonth(string date)Returns the month part of a date or a timestamp string: month(“1970-11-01 00:00:00”) = 11, month(“1970-11-01”) = 11.intday(string date) dayofmonth(date)Returns the day part of a date or a timestamp string: day(“1970-11-01 00:00:00”) = 1, day(“1970-11-01”) = 1.inthour(string date)Returns the hour of the timestamp: hour(‘2009-07-30 12:58:59’) = 12, hour(‘12:58:59’) = 12.intminute(string date)Returns the minute of the timestamp.intsecond(string date)Returns the second of the timestamp.intweekofyear(string date)Returns the week number of a timestamp string: weekofyear(“1970-11-01 00:00:00”) = 44, weekofyear(“1970-11-01”) = 44.intextract(field FROM source)Retrieve fields such as days or hours from source (as of Hive 2.2.0). Source must be a date, timestamp, interval or a string that can be converted into either a date or timestamp. Supported fields include: day, dayofweek, hour, minute, month, quarter, second, week and year.Examples:select extract(month from “2016-10-20”) results in 10.select extract(hour from “2016-10-20 05:06:07”) results in 5.select extract(dayofweek from “2016-10-20 05:06:07”) results in 5.select extract(month from interval ‘1-3’ year to month) results in 3.select extract(minute from interval ‘3 12:20:30’ day to second) results in 20.intdatediff(string enddate, string startdate)Returns the number of days from startdate to enddate: datediff(‘2009-03-01’, ‘2009-02-27’) = 2.pre 2.1.0: string2.1.0 on: datedate_add(date/timestamp/string startdate, tinyint/smallint/int days)Adds a number of days to startdate: date_add(‘2008-12-31’, 1) = ‘2009-01-01’.Prior to Hive 2.1.0 (HIVE-13248) the return type was a String because no Date type existed when the method was created.pre 2.1.0: string2.1.0 on: datedate_sub(date/timestamp/string startdate, tinyint/smallint/int days)Subtracts a number of days to startdate: date_sub(‘2008-12-31’, 1) = ‘2008-12-30’.Prior to Hive 2.1.0 (HIVE-13248) the return type was a String because no Date type existed when the method was created.timestampfrom_utc_timestamp({any primitive type} ts, string timezone)Converts a timestamp* in UTC to a given timezone (as of Hive 0.8.0).* timestamp is a primitive type, including timestamp/date, tinyint/smallint/int/bigint, float/double and decimal. Fractional values are considered as seconds. Integer values are considered as milliseconds. For example, from_utc_timestamp(2592000.0,‘PST’), from_utc_timestamp(2592000000,‘PST’) and from_utc_timestamp(timestamp ‘1970-01-30 16:00:00’,‘PST’) all return the timestamp 1970-01-30 08:00:00.timestampto_utc_timestamp({any primitive type} ts, string timezone)Converts a timestamp* in a given timezone to UTC (as of Hive 0.8.0).* timestamp is a primitive type, including timestamp/date, tinyint/smallint/int/bigint, float/double and decimal.Fractional values are considered as seconds. Integer values are considered as milliseconds. For example, to_utc_timestamp(2592000.0,‘PST’), to_utc_timestamp(2592000000,‘PST’) and to_utc_timestamp(timestamp ‘1970-01-30 16:00:00’,‘PST’) all return the timestamp 1970-01-31 00:00:00.datecurrent_dateReturns the current date at the start of query evaluation (as of Hive 1.2.0). All calls of current_date within the same query return the same value.timestampcurrent_timestampReturns the current timestamp at the start of query evaluation (as of Hive 1.2.0). All calls of current_timestamp within the same query return the same value.stringadd_months(string start_date, int num_months, output_date_format)Returns the date that is num_months after start_date (as of Hive 1.1.0). start_date is a string, date or timestamp. num_months is an integer. If start_date is the last day of the month or if the resulting month has fewer days than the day component of start_date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as start_date. The default output format is ‘yyyy-MM-dd’.Before Hive 4.0.0, the time part of the date is ignored.As of Hive 4.0.0, add_months supports an optional argument output_date_format, which accepts a String that represents a valid date format for the output. This allows to retain the time format in the output.For example :add_months(‘2009-08-31’, 1) returns ‘2009-09-30’. add_months(‘2017-12-31 14:15:16’, 2, ‘YYYY-MM-dd HH:mm:ss’) returns ‘2018-02-28 14:15:16’.stringlast_day(string date)Returns the last day of the month which the date belongs to (as of Hive 1.1.0). date is a string in the format ‘yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’. The time part of date is ignored.stringnext_day(string start_date, string day_of_week)Returns the first date which is later than start_date and named as day_of_week (as of Hive 1.2.0). start_date is a string/date/timestamp. day_of_week is 2 letters, 3 letters or full name of the day of the week (e.g. Mo, tue, FRIDAY). The time part of start_date is ignored. Example: next_day(‘2015-01-14’, ‘TU’) = 2015-01-20.stringtrunc(string date, string format)Returns date truncated to the unit specified by the format (as of Hive 1.2.0). Supported formats: MONTH/MON/MM, YEAR/YYYY/YY. Example: trunc(‘2015-03-17’, ‘MM’) = 2015-03-01.doublemonths_between(date1, date2)Returns number of months between dates date1 and date2 (as of Hive 1.2.0). If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise the UDF calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2. date1 and date2 type can be date, timestamp or string in the format ‘yyyy-MM-dd’ or ‘yyyy-MM-dd HH:mm:ss’. The result is rounded to 8 decimal places. Example: months_between(‘1997-02-28 10:30:00’, ‘1996-10-30’) = 3.94959677stringdate_format(date/timestamp/string ts, string fmt)Converts a date/timestamp/string to a value of string in the format specified by the date format fmt (as of Hive 1.2.0). Supported formats are Java DateTimeFormatter formats – https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html. The second argument fmt should be constant. Example: date_format(‘2015-04-08’, ‘y’) = ‘2015’.date_format can be used to implement other UDFs, e.g.:dayname(date) is date_format(date, ‘EEEE’)dayofyear(date) is date_format(date, ‘D’)Prior to Hive 4.0.0 (HIVE-25458), it uses [https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html] and hence the supported patterns have changed.


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #hive的时间函数 #unixtime #string #format将 #Unix #纪元