Hive学习—hive函数学习

hive函数学习

一、关系运算:

=、<>、<、>、<=、>=、IS NULL、IS NOT NULL、LIKE、RLIKE、REGEXP
其中:RLIKE
语法: A RLIKE B
操作类型: strings
描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE;否则为FALSE。
举例:
hive> select 1 from lxw_dual where 'footbar’ rlike ‘^f.*rKaTeX parse error: Got function '\newline' with no arguments as superscript at position 75: …123456' rlike '^̲\\d+’;
1

二、数学运算:

+、-、*、/、%、&、|、^、~
注意: 精度在hive中是个很大的问题,可以通过round指定精度
hive > select round(8.4%4,2) from lxw_dual;
0.4

三、逻辑运算

AND、OR、NOT

四、数值计算:

1. 取整函数:round
语法: round(double a)
返回值: BIGINT
说明: 返回double类型的整数值部分 (遵循四舍五入)
举例:
hive> select round(3.1415926) from lxw_dual;
3
hive> select round(3.5) from lxw_dual;
4
2. 指定精度取整函数:round
语法: round(double a, int d)
返回值: DOUBLE
说明: 返回指定精度d的double类型
举例:
hive> select round(3.1415926,4) from lxw_dual;
3.1416
3. 向下取整函数: floor
语法: floor(double a)
返回值: BIGINT
说明: 返回等于或者小于该double变量的最大的整数
4. 向上取整函数: ceil
语法: ceil(double a)
返回值: BIGINT
说明: 返回等于或者大于该double变量的最小的整数
5. 向上取整函数: ceiling
6. 取随机数函数:rand
语法: rand(),rand(int seed)
返回值: double
说明: 返回一个0到1范围内的随机数。如果指定种子seed,则会等到一个稳定的随机数序列
hive> select rand() from lxw_dual;
0.5577432776034763
hive> select rand() from lxw_dual;
0.6638336467363424
hive> select rand(100) from lxw_dual;
0.7220096548596434
hive> select rand(100) from lxw_dual;
0.7220096548596434
7. 自然指数函数:exp
8. 以10为底对数函数: log10
9. 以2为底对数函数: log2
10. 对数函数: log
语法: log(double base, double a)
返回值: double
说明: 返回以base为底的a的对数
11. 幂运算函数: pow
语法: pow(double a, double p)
返回值: double
说明: 返回a的p次幂
12. 幂运算函数: power
13. 开平方函数: sqrt
14. 二进制函数: bin
15. 十六进制函数: hex
16. 反转十六进制函数: unhex
17. 进制转换函数: conv
语法: conv(BIGINT num, int from_base, int to_base)
返回值: string
说明: 将数值num从from_base进制转化到to_base进制
举例:
hive> select conv(17,10,16) from lxw_dual;
11
hive> select conv(17,10,2) from lxw_dual;
10001
18 绝对值函数: abs
19. 正取余函数: pmod
语法: pmod(int a, int b),pmod(double a, double b)
返回值: int double
说明: 返回正的a除以b的余数
20. 正弦函数: sin
21. 反正弦函数: asin
22. 余弦函数: cos
23. 反余弦函数: acos
24. positive函数: positive
语法: positive(int a), positive(double a)
返回值: int double
说明: 返回a
25. negative函数: negative
语法: negative(int a), negative(double a)
返回值: int double
说明: 返回-a

五、日期函数

1. UNIX时间戳转日期函数:from_unixtime
语法: from_unixtime(bigint unixtime[, string format])
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
hive> select from_unixtime(1323308943,‘yyyyMMdd’) from lxw_dual;
20111208
2. 获取当前UNIX时间戳函数:unix_timestamp
语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
hive> select unix_timestamp() from lxw_dual;
1323309615
3. 日期转UNIX时间戳:unix_timestamp
语法: unix_timestamp(string date)
返回值: bigint
说明: 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp(‘2011-12-07 13:01:03’) from lxw_dual;
1323234063
4. 指定格式日期转UNIX时间戳函数:unix_timestamp
语法: unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp(‘20111207 13:01:03’,‘yyyyMMdd HH:mm:ss’) from lxw_dual;
1323234063
5. 日期时间转日期函数: to_date
语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。举例:hive> select to_date(‘2011-12-08 10:03:01’) from lxw_dual;2011-12-08
6. 日期转年函数: year
语法: year(string date)
返回值: int
说明: 返回日期中的年。
举例:
hive> select year(‘2011-12-08 10:03:01’) from lxw_dual;
2011
7. 日期转月函数: month
8. 日期转天函数: day
9. 日期转小时函数: hour
10. 日期转分钟函数: minute
11. 日期转秒函数: second
12. 日期转周函数: weekofyear
语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。
13. 日期比较函数: datediff
14. 日期增加函数: date_add
语法: date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:hive> select date_add(‘2012-12-08’,10) from lxw_dual;2012-12-18
15. 日期减少函数: date_sub

六、条件函数

1. if函数: if
语法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T说明: 当条件testCondition为TRUE时,返回valueTrue;否则返回valueFalseOrNull
举例:
hive> select if(1=2,100,200) from lxw_dual;
200
hive> select if(1=1,100,200) from lxw_dual;
100
2. 非空查找函数: COALESCE
语法: COALESCE(T v1, T v2, …)
返回值: T说明: 返回参数中的第一个非空值;如果所有值都为NULL,那么返回NULL
举例:
hive> select COALESCE(null,‘100’,'50′) from lxw_dual;
100
3. 条件判断函数:CASE
语法: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END返回值: T
说明:如果a等于b,那么返回c;如果a等于d,那么返回e;否则返回f
举例:
hive> Select case 100 when 50 then ‘tom’ when 100 then ‘mary’ else ‘tim’ end from lxw_dual;
mary
4. 条件判断函数:CASE
语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
返回值: T说
明:如果a为TRUE,则返回b;如果c为TRUE,则返回d;否则返回e

七、字符串函数

1. 字符串长度函数:length
2. 字符串反转函数:reverse
3. 字符串连接函数:concat
语法: concat(string A, string B…)
返回值: string
说明:返回输入字符串连接后的结果,支持任意个输入字符串
举例:
hive> select concat(‘abc’,‘def’,‘gh’) from lxw_dual;
abcdefgh
4. 带分隔符字符串连接函数:concat_ws
语法: concat_ws(string SEP, string A, string B…)
返回值: string
说明:返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符
举例:
hive> select concat_ws(’,’,‘abc’,‘def’,‘gh’) from lxw_dual;
abc,def,gh
5. 字符串截取函数:substr,substring
语法: substr(string A, int start),substring(string A, int start)
返回值: string
说明:返回字符串A从start位置到结尾的字符串
举例:
hive> select substr(‘abcde’,3) from lxw_dual;
cde
hive> select substring(‘abcde’,3) from lxw_dual;
cde
hive> select substr(‘abcde’,-1) from lxw_dual; (和ORACLE相同)
e
6. 字符串截取函数:substr,substring
语法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string说
明:返回字符串A从start位置开始,长度为len的字符串
举例:
hive> select substr(‘abcde’,3,2) from lxw_dual;
cd
hive>select substring(‘abcde’,-2,2) from lxw_dual;
de
7. 字符串转大写函数:upper,ucase
语法: upper(string A) ucase(string A)
返回值: string
说明:返回字符串A的大写格式
8. 字符串转小写函数:lower,lcase
9. 去空格函数:trim
语法: trim(string A)
返回值: string
说明:去除字符串两边的空格
10. 左边去空格函数:ltrim
11. 右边去空格函数:rtrim
12. 正则表达式替换函数:regexp_replace
语法: regexp_replace(string A, string B, string C)
返回值: string
说明:将字符串A中的符合java正则表达式B的部分替换为C。
举例:
hive> select regexp_replace(‘foobar’, ‘oo|ar’, ‘’) from lxw_dual;
fb
13. 正则表达式解析函数:regexp_extract
14. URL解析函数:parse_url
15. json解析函数:get_json_object
语法:get_json_object(string json_string,string path)
返回值:string
说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。
举例:
hive> select get_json_object(’{“store”: {“fruit”:[{“weight”:8,“type”:“apple”},{“weight”:9,“type”:“pear”}], “bicycle”:{“price”:19.95,“color”:“red”} }, “email”:“amy@only_for_json_udf_test.net”, “owner”:“amy”} ‘,’$.owner’) from lxw_dual;
amy
16. 空格字符串函数:space
17. 重复字符串函数:repeat
18. 首字符ascii函数:ascii
19. 左补足函数:lpad
语法: lpad(string str, int len, string pad)
返回值: string
说明:将str进行用pad进行左补足到len位
举例:
hive> select lpad(‘abc’,10,‘td’) from lxw_dual;tdtdtdtabc
20. 右补足函数:rpad
21. 分割字符串函数: split
语法: split(string str, string pat)
返回值: array
说明: 按照pat字符串分割str,会返回分割后的字符串数组
举例:
hive> select split(‘abtcdtef’,‘t’) from lxw_dual;[“ab”,“cd”,“ef”]
22. 集合查找函数: find_in_set

八、集合统计函数

count、sum、avg、min、max
** 6. 非空集合总体变量函数: var_pop**
** 7. 非空集合样本变量函数: var_samp**
8. 总体标准偏离函数: stddev_pop
9. 样本标准偏离函数: stddev_samp
10. 中位数函数: percentile

十、复杂类型访问操作

1. array类型访问:A[n]
2.map类型访问:M[key]
3.struct类型访问:S.x

猜你喜欢

转载自blog.csdn.net/weixin_43387060/article/details/86765718
今日推荐