ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Hive内置函数

2021-06-05 16:03:46  阅读:431  来源: 互联网

标签:Function 内置 函数 hadoop hive BUILTIN Hive apache org



!a - 逻辑非,与非逻辑操作符号一致性

hive> select !(true);
OK
false
!=
a != b - 如果 a 不等于 b,则返回 TRUE,和<>符操作符一致

hive> select 1 <> 2;
OK
true
$sum0
$sum0(x) - 返回一组数字的总和,如果没有数字为空范围0

hive> select $sum0(1L);
OK
1
%
a % b - 返回 a 除以 b 时的余数

&
a & b - 按位和

hive> SELECT 3 & 5 FROM src LIMIT 1;
1
*
a * b - 将 a 乘以 b

a + b - 返回 a+b

——
a - b - 返回差值 ab

/
a / b - 除以 b

SELECT 3 / 2 FROM src LIMIT 1;
1.5
<
a < b - 如果 a 小于 b,则返回 TRUE

<=
a <= b - 如果 a 不大于 b,则返回 TRUE

<=>
a <=> b - 对于非空操作数,使用 EQUAL(=) 运算符返回相同的结果,但如果两者都为 NULL,则返回 TRUE,如果其中一个为 NULL,则返回 FALSE

<>
a <> b - 如果 a 不等于 b,则返回 TRUE,和!= 含义一致

=
a = b - 如果 a 等于 b 则返回 TRUE,否则返回 false,和==一致一致

==
a == b - 如果 a 等于 b 则返回 TRUE,否则返回 false,和 = 含义一致

a > b - 如果 a 大于 b,则返回 TRUE

=
a >= b - 如果 a 不小于 b,则返回 TRUE

^
a ^ b - 按位异或

SELECT 3 ^ 5 FROM src LIMIT 1;
2
腹肌
abs(x) - 返回 x 的绝对值

SELECT abs(0) FROM src LIMIT 1;
0
SELECT abs(-5) FROM src LIMIT 1;
5
阿科斯
acos(x) - 如果 -1<=x<=1 或 NULL 则返回 x 的反余弦值,否则返回

SELECT acos(1) FROM src LIMIT 1;
0
SELECT acos(2) FROM src LIMIT 1;
NULL
添加_月
add_months(start_date, num_months) - 返回 start_date 之后 num_months 的日期。
start_date 是格式为“yyyy-MM-dd HH:mm:ss”或“yyyy-MM-dd”的字符串。num_months 是一个数字。start_date 的时间部分被忽略。

SELECT add_months(‘2009-08-31’, 1) FROM src LIMIT 1;
‘2009-09-30’
aes_decrypt
aes_decrypt(input binary, key string/binary) - 使用 AES 解密输入。
AES(高级加密标准)算法。可以使用 128、192 或 256 位的密钥长度。如果安装了 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files,则可以使用 192 位和 256 位密钥。如果任一参数为 NULL 或密钥长度不是允许的值之一,则返回值为 NULL。

SELECT aes_decrypt(unbase64(‘y6Ss+zCYObpCbgfWfyNWTw==’), ‘1234567890123456’);
‘ABC’
aes_encrypt
aes_encrypt(input string/binary, key string/binary) - 使用 AES 加密输入。
AES(高级加密标准)算法。可以使用 128、192 或 256 位的密钥长度。如果安装了 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files,则可以使用 192 位和 256 位密钥。如果任一参数为 NULL 或密钥长度不是允许的值之一,则返回值为 NULL。

SELECT base64(aes_encrypt(‘ABC’, ‘1234567890123456’));
‘y6Ss+zCYObpCbgfWfyNWTw==’

a1 and a2 and … and an - 逻辑和

大批
array(n0, n1…) - 用给定的元素创建一个数组

数组包含
array_contains(array, value) - 如果数组包含值,则返回 TRUE。

SELECT array_contains(array(1, 2, 3), 2) FROM src LIMIT 1;
true
ASCII码
ascii(str) - 返回 str 的第一个字符的数值
如果 str 为空则返回 0 如果 str 为 NULL 则返回 NULL

SELECT ascii(‘222’) FROM src LIMIT 1; 50
SELECT ascii(2) FROM src LIMIT 1;
50
阿信
asin(x) - 如果 -1<=x<=1 或 NULL,则返回 x 的反正弦

SELECT asin(0) FROM src LIMIT 1;
0
SELECT asin(2) FROM src LIMIT 1;
NULL
断言真
assert_true(condition) - Throw an exception if ‘condition’ is not true.

SELECT assert_true(x >= 0) FROM src LIMIT 1;
NULL
assert_true_oom
assert_true_oom - assertation failed; Simulated OOM

select assert_true_oom(${hiveconf:zzz}>sum(1)) from tu join tv on (tu.id_uv=tv.id_uv) where u<10 and v>1
atan
atan(x) - returns the atan (arctan) of x (x is in radians)

SELECT atan(0) FROM src LIMIT 1;
0
avg
avg(x) - Returns the mean of a set of numbers

base64
base64(bin) - Convert the argument from binary to a base 64 string

between
between a [NOT] BETWEEN b AND c - evaluate if a is [not] in between b and c

bin
bin(n) - returns n in binary
n is a BIGINT. Returns NULL if n is NULL.

SELECT bin(13) FROM src LIMIT 1
‘1101’
bloom_filter
Generic UDF to generate Bloom Filter

bround
bround(x[, d]) - round x to d decimal places using HALF_EVEN rounding mode.
Banker’s rounding. The value is rounded to the nearest even number. Also known as Gaussian rounding.

SELECT bround(12.25, 1);
12.2
cardinality_violation
cardinality_violation(n0, n1…) - raises Cardinality Violation

case
CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END - 当 a = b 时,返回 c;当 a = d 时,返回 e;否则返回 f

SELECT
CASE deptno
WHEN 1 THEN Engineering
WHEN 2 THEN Finance
ELSE admin
END,
CASE zone
WHEN 7 THEN Americas
ELSE Asia-Pac
END
FROM emp_details
CBRT
cbrt(double) - 返回双精度值的立方根。

SELECT cbrt(27.0);
3.0
细胞
ceil(x) - 找到不小于 x 的最小整数
同义词:ceiling

SELECT ceil(-0.1) FROM src LIMIT 1;
0
SELECT ceil(5) FROM src LIMIT 1;
5
天花板
天花板(x) - 找到不小于 x 的最小整数
同义词:ceil

SELECT ceiling(-0.1) FROM src LIMIT 1;
0
SELECT ceiling(5) FROM src LIMIT 1;
5
字符长度
char_length(str | binary) - 返回 str 或二进制数据中的字符数

字符长度
character_length(str | binary) - 返回 str 或二进制数据中的字符数

色度
chr(str) - 将 n 其中 n : [0, 256) 转换为相当于 varchar 的 ascii。如果 n 小于 0,则返回空字符串。如果 n > 256,则返回 chr(n % 256)。

SELECT chr(‘48’) FROM src LIMIT 1;
‘0’
SELECT chr(‘65’) FROM src LIMIT 1;
‘A’
合并
coalesce(a1, a2, …) - 返回第一个非空参数

SELECT coalesce(NULL, 1, NULL) FROM src LIMIT 1;
1
收集清单
collect_list(x) - 返回具有重复项的对象列表

收集集
collect_set(x) - 返回一组消除了重复元素的对象

计算统计
compute_stats(x) - 返回一组原始类型值的统计摘要。

连接
concat(str1, str2, … strN) - 返回 str1, str2, … strN 或 concat(bin1, bin2, … binN) 的串联 - 返回二进制数据 bin1, bin2, … 中字节的串联。 … binN
如果任何参数为 NULL,则返回 NULL。

SELECT concat(‘abc’, ‘def’) FROM src LIMIT 1;
‘abcdef’
concat_ws
concat_ws(separator, [string | array(string)]+) - 返回由分隔符分隔的字符串的连接。

SELECT concat_ws(’.’, ‘www’, array(‘iteblog’, ‘com’)) FROM src LIMIT 1;
‘www.iteblog.com’
context_ngrams
context_ngrams(expr, array<string1 , string2, …>, k, pf) 估计适合指定上下文的前 k 个最频繁的 n-gram。第二个参数指定一串单词,用于指定 n-gram 元素的位置,空值代表必须由 n-gram 元素填充的“空白”。
主表达式必须是一个字符串数组,或者一个字符串数组数组,例如sentence() UDF 的返回类型。第二个参数指定上下文——例如,array(“i”, “love”, null)——它将估计主要表达式中短语“i love”之后的前“k”个单词。可选的第四个参数“pf”控制启发式使用的内存。较大的值会产生更好的准确性,但会使用更多的内存。

SELECT context_ngrams(sentences(lower(review)), array(“i”, “love”, null, null), 10) FROM movies
将尝试在自由格式的自然语言电影评论数据库中确定 10 个最常见的两个词短语,跟在“我爱”之后。

转化率
conv(num, from_base, to_base) - 将 num 从 from_base 转换为 to_base
如果 to_base 为负数,则将 num 视为有符号整数,否则,将其视为无符号整数。

SELECT conv(‘100’, 2, 10) FROM src LIMIT 1;
‘4’
SELECT conv(-10, 16, -10) FROM src LIMIT 1;
‘16’
校正
corr(x,y) - 返回一组数字对之间的皮尔逊相关系数

The function takes as arguments any pair of numeric types and returns a double.
Any pair with a NULL is ignored. If the function is applied to an empty set or
a singleton set, NULL will be returned. Otherwise, it computes the following:
COVAR_POP(x,y)/(STDDEV_POP(x)*STDDEV_POP(y))
where neither x nor y is null,
COVAR_POP is the population covariance,
and STDDEV_POP is the population standard deviation.
cos
cos(x) - 返回 x 的余弦值(x 以弧度为单位)

SELECT cos(0) FROM src LIMIT 1;
1
数数
count(*) - 返回检索到的总行数,包括包含 NULL 值的行。
count(expr) - 返回提供的表达式为非 NULL 的行数。
count(DISTINCT expr[, expr…]) - 返回提供的表达式唯一且非 NULL 的行数。

covar_pop
covar_pop(x,y) - 返回一组数字对的总体协方差
该函数将任何数字类型对作为参数并返回一个双精度值。
任何带有 NULL 的对都将被忽略。如果该函数应用于空集,
则将返回NULL 。否则,它将计算以下内容:

(SUM(x*y)-SUM(x)*SUM(y)/COUNT(x,y))/COUNT(x,y)
其中 x 和 y 都不为空。

covar_samp
covar_samp(x,y) - 返回一组数字对的样本协方差
该函数将任何数字类型对作为参数并返回一个双精度值。
任何带有 NULL 的对都将被忽略。如果该函数应用于空集,
则将返回NULL 。否则,它将计算以下内容:

(SUM(x*y)-SUM(x)*SUM(y)/COUNT(x,y))/(COUNT(x,y)-1)
其中 x 和 y 都不为空。

CRC32
crc32(str or bin) - 计算字符串或二进制参数的循环冗余校验值并返回 bigint 值。

SELECT crc32(‘ABC’);
2743272264
SELECT crc32(binary(‘ABC’));
2743272264
create_union
create_union(tag, obj1, obj2, obj3, …) - Creates a union with the object for given tag

SELECT create_union(1, 1, “one”) FROM src LIMIT 1;
{1:“one”}
cume_dist
cume_dist - The CUME_DIST function (defined as the inverse of percentile in some statistical books) computes the position of a specified value relative to a set of values. To compute the CUME_DIST of a value x in a set S of size N, you use the formula: CUME_DIST(x) = number of values in S coming before and including x in the specified order/ N

Function class:org.apache.hadoop.hive.ql.udf.generic.GenericUDAFCumeDist
Function type:BUILTIN

current_authorizer
current_authorizer() - Returns the current authorizer (class name of the authorizer).
Function class:org.apache.hadoop.hive.ql.udf.generic.GenericUDFCurrentAuthorizer
Function type:BUILTIN

current_database
current_database() - returns currently using database name

current_date
返回系统当前日期

hive> select current_date();
OK
2017-02-22
current_groups
current_groups() - Returns all groups the current user belongs to.

current_timestamp
返回系统当前时间

hive> select current_timestamp();
OK
2017-02-22 14:04:33.332
current_user
current_user() - 返回当前用户名称,SessionState UserFromAuthenticator

hive> select current_user();
OK
iteblog
date_add
date_add(start_date, num_days) - Returns the date that is num_days after start_date.
start_date is a string in the format ‘yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’. num_days is a number. The time part of start_date is ignored.

SELECT date_add(‘2009-07-30’, 1) FROM src LIMIT 1;
‘2009-07-31’
date_format
date_format(date/timestamp/string, fmt) - converts a date/timestamp/string to a value of string in the format specified by the date format fmt.
Supported formats are SimpleDateFormat formats - https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html. Second argument fmt should be constant.

SELECT date_format(‘2015-04-08’, ‘y’);
‘2015’
date_sub
date_sub(start_date, num_days) - Returns the date that is num_days before start_date.
start_date is a string in the format ‘yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’. num_days is a number. The time part of start_date is ignored.

SELECT date_sub(‘2009-07-30’, 1) FROM src LIMIT 1;
‘2009-07-29’
datediff
datediff(date1, date2) - Returns the number of days between date1 and date2
date1 and date2 are strings in the format ‘yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’. The time parts are ignored.If date1 is earlier than date2, the result is negative.

SELECT datediff(‘2009-07-30’, ‘2009-07-31’) FROM src LIMIT 1;
1
day
day(param) - Returns the day of the month of date/timestamp, or day component of interval
Synonyms: dayofmonth
param can be one of:

A string in the format of ‘yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’.
A date value
A timestamp value
A day-time interval value

SELECT day(‘2009-07-30’) FROM src LIMIT 1;
30
dayofmonth
dayofmonth(param) - Returns the day of the month of date/timestamp, or day component of interval
Synonyms: day
param can be one of:

A string in the format of ‘yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’.
A date value
A timestamp value
A day-time interval value

SELECT dayofmonth(‘2009-07-30’) FROM src LIMIT 1;
30
dayofweek
dayofweek(param) - 返回日期/时间戳的星期几(1 = 星期日,2 = 星期一,…,7 = 星期六)
参数可以是以下之一:

‘yyyy-MM-dd HH:mm:ss’ 或 ‘yyyy-MM-dd’ 格式的字符串。
日期值
时间戳值示例:

SELECT dayofweek(‘2009-07-30’) FROM src LIMIT 1;
5
函数类:org.apache.hadoop.hive.ql.udf.UDFDayOfWeek
函数类型:BUILTIN

解码
decode(bin, str) - 使用第二个参数字符集解码第一个参数字符集的
可能选项有“US-ASCII”、“ISO-8859-1”、
“UTF-8”、“UTF-16BE”、 “UTF-16LE”和“UTF-16”。如果任一参数
为空,则结果也将为空

度数
degree(x) - 将弧度转换为度数

SELECT degrees(30) FROM src LIMIT 1;
-1
密集等级
没有函数“dense_rank”的文档

div
a div b - 将 a 除以 b 四舍五入为长整数

SELECT 3 div 2 FROM src LIMIT 1;
1
电子
e() - 返回 E

SELECT e() FROM src LIMIT 1;
2.718281828459045
埃尔特
elt(n, str1, str2, …) - 返回第 n 个字符串

SELECT elt(1, ‘face’, ‘book’) FROM src LIMIT 1;
‘face’
编码
encode(str, str) - 使用第二个参数字符集对第一个参数进行编码 字符集的
可能选项有“US-ASCII”、“ISO-8859-1”、
“UTF-8”、“UTF-16BE”、 “UTF-16LE”和“UTF-16”。如果任一参数
为空,则结果也将为空

强制约束
强制约束(x) - 内部 UDF 来强制执行 CHECK 和 NOT NULL 约束
仅供内部使用
函数类:org.apache.hadoop.hive.ql.udf.generic.GenericUDFEnforceConstraint
函数类型:BUILTIN

ewah_位图
ewah_bitmap(expr) - 返回一列的 EWAH 压缩位图表示。

ewah_bitmap_and
ewah_bitmap_and(b1, b2) - 返回一个 EWAH 压缩位图,它是两个位图的按位与。

ewah_bitmap_empty
ewah_bitmap_empty(bitmap) - 测试 EWAH 压缩位图是否全为零的谓词

ewah_bitmap_or
ewah_bitmap_or(b1, b2) - 返回一个 EWAH 压缩位图,它是两个位图的按位 OR。

经验值
exp(x) - 返回 e 的 x 次方

SELECT exp(0) FROM src LIMIT 1;
1
爆炸
explode(a) - 将数组 a 的元素分成多行,或将地图的元素分成多行和多列

阶乘
factorial(int) - 返回 n 阶乘。有效的 n 是 [0…20]。
如果 n 超出 [0…20] 范围,则返回 null。

SELECT factorial(5);
120
提取联合
extract_union(union[, tag]) - 递归地将联合分解为结构体或简单地提取给定的标签。

SELECT extract_union({0:“foo”}).tag_0 FROM src;
foo
SELECT extract_union({0:“foo”}).tag_1 FROM src;
null
SELECT extract_union({0:“foo”}, 0) FROM src;
foo
SELECT extract_union({0:“foo”}, 1) FROM src;
null
函数类:org.apache.hadoop.hive.ql.udf.generic.GenericUDFExtractUnion
函数类型:BUILTIN

阶乘
factorial(int) - Returns n factorial. Valid n is [0…20].
Returns null if n is out of [0…20] range.
Example:

SELECT factorial(5);
120
Function class:org.apache.hadoop.hive.ql.udf.generic.GenericUDFFactorial
Function type:BUILTIN

field
field(str, str1, str2, …) - returns the index of str in the str1,str2,… list or 0 if not found
All primitive types are supported, arguments are compared using str.equals(x). If str is NULL, the return value is 0.

find_in_set
find_in_set(str,str_array) - Returns the first occurrence of str in str_array where str_array is a comma-delimited string. Returns null if either argument is null. Returns 0 if the first argument has any commas.

SELECT find_in_set(‘ab’,‘abc,b,ab,c,def’) FROM src LIMIT 1;
3
SELECT * FROM src1 WHERE NOT find_in_set(key,‘311,128,345,956’) = 0;
311 val_311
128
first_value
first_value - This takes at most two parameters. The first parameter is the column for which you want the first value, the second (optional) parameter must be a boolean which is false by default. If set to true it skips null values.

floor
floor(x) - Find the largest integer not greater than x

SELECT floor(-0.1) FROM src LIMIT 1;
-1
SELECT floor(5) FROM src LIMIT 1;
5
floor_day
floor_day(param) - Returns the timestamp at a day granularity
param needs to be a timestamp value
Example:

SELECT floor_day(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-MM-dd 00:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorDay
Function type:BUILTIN

floor_hour
floor_hour(param) - Returns the timestamp at a hour granularity
param needs to be a timestamp value
Example:

SELECT floor_hour(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-MM-dd HH:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorHour
Function type:BUILTIN

floor_minute
floor_minute(param) - Returns the timestamp at a minute granularity
param needs to be a timestamp value
Example:

SELECT floor_minute(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-MM-dd HH:mm:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorMinute
Function type:BUILTIN

floor_month
floor_month(param) - Returns the timestamp at a month granularity
param needs to be a timestamp value
Example:

SELECT floor_month(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-MM-01 00:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorMonth
Function type:BUILTIN

floor_quarter
floor_quarter(param) - Returns the timestamp at a quarter granularity
param needs to be a timestamp value
Example:

SELECT floor_quarter(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-xx-01 00:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorQuarter
Function type:BUILTIN

floor_second
floor_second(param) - Returns the timestamp at a second granularity
param needs to be a timestamp value
Example:

SELECT floor_second(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-MM-dd HH:mm:ss
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorSecond
Function type:BUILTIN

floor_week
floor_week(param) - Returns the timestamp at a week granularity
param needs to be a timestamp value
Example:

SELECT floor_week(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-MM-xx 00:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorWeek
Function type:BUILTIN

floor_year
floor_year(param) - Returns the timestamp at a year granularity
param needs to be a timestamp value
Example:

SELECT floor_year(CAST(‘yyyy-MM-dd HH:mm:ss’ AS TIMESTAMP)) FROM src;
yyyy-01-01 00:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorYear
Function type:BUILTIN

format_number
format_number(X, D or F) - Formats the number X to a format like ‘#,###,###.##’, rounded to D decimal places, Or Uses the format specified F to format, and returns the result as a string. If D is 0, the result has no decimal point or fractional part. This is supposed to function like MySQL’s FORMAT

SELECT format_number(12332.123456, 4) FROM src LIMIT 1;
‘12,332.1235’
SELECT format_number(12332.123456, ‘##################.###’) FROM src LIMIT 1;
‘12332.123’
from_unixtime
from_unixtime(unix_time, format) - returns unix_time in the specified format

SELECT from_unixtime(0, ‘yyyy-MM-dd HH:mm:ss’) FROM src LIMIT 1;
‘1970-01-01 00:00:00’
from_utc_timestamp
from_utc_timestamp(timestamp, string timezone) - Assumes given timestamp is UTC and converts to given timezone (as of Hive 0.8.0)

get_json_object
get_json_object(json_txt, path) - Extract a json object from path

Extract json object from a json string based on json path specified, and return json string of the extracted json object. It will return null if the input json string is invalid.
A limited version of JSONPath supported:
$ : Root object
. : Child operator
[] : Subscript operator for array

  • : Wildcard for []
    Syntax not supported that’s worth noticing:
    ‘’ : Zero length string as key
    … : Recursive descent
    &#064; : Current object/element
    () : Script expression
    ?() : Filter (script) expression.
    [,] : Union operator
    [start

    标签:Function,内置,函数,hadoop,hive,BUILTIN,Hive,apache,org
    来源: https://blog.csdn.net/m0_56218665/article/details/117597871

    本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
    2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
    3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
    4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
    5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有