浮云飘飘

一、关系运算：

1. 等值比较: =

语法：A=B

操作类型：所有基本类型

描述如果表达式A与表达式B相等，则为TRUE；否则为FALSE

举例：

hive>select 1 from lxw_dual where 1=1;

1

2. 不等值比较: <>

语法: A <> B

hive> select1 from lxw_dual where 1 <> 2;

1

3. 小于比较: <

语法: A < B

hive> select1 from lxw_dual where 1 < 2;

1

4. 小于等于比较: <=

hive> select1 from lxw_dual where 1 <= 1;

1

5. 大于比较: >

hive> select1 from lxw_dual where 2 > 1;

1

6. 大于等于比较: >=

hive> select1 from lxw_dual where 1 >= 1;

1

hive> select* from lxw_dual;

201111120900:00:00     2011111209

hive> selecta,b,a<b,a>b,a=b from lxw_dual;

201111120900:00:00     2011111209      false   true    false

7. 空值判断: IS NULL

hive> select1 from lxw_dual where null is null;

1

8. 非空判断: IS NOTNULL

hive> select1 from lxw_dual where 1 is not null;

1

9. LIKE比较: LIKE

hive> select1 from lxw_dual where 'football' like 'foot%';

1

hive> select1 from lxw_dual where 'football' like 'foot____';

1

hive> select1 from lxw_dual where NOT 'football' like 'fff%';

1

10. JAVA的LIKE操作: RLIKE

hive> select1 from lxw_dual where 'footbar’ rlike '^f.*r\$’;

1

hive>select 1from lxw_dual where '123456' rlike '^\\d+\$';

1

hive> select1 from lxw_dual where '123456aa' rlike '^\\d+\$';

11. REGEXP操作: REGEXP

hive> select1 from lxw_dual where 'footbar' REGEXP '^f.*r\$';

1

二、数学运算：

1. 加法操作: +

hive> select1 + 9 from lxw_dual;

10

hive> createtable lxw_dual as select 1 + 1.2 from lxw_dual;

hive> describelxw_dual;

_c0     double

2. 减法操作: -

hive> select10 – 5 from lxw_dual;

5

hive> createtable lxw_dual as select 5.6 – 4 from lxw_dual;

hive>describe lxw_dual;

_c0     double

3. 乘法操作: *

hive> select40 * 5 from lxw_dual;

200

4. 除法操作: /

hive> select40 / 5 from lxw_dual;

8.0

hive>select ceil(28.0/6.999999999999999999999) from lxw_duallimit 1;

hive>select ceil(28.0/6.99999999999999) from lxw_dual limit1;

5. 取余操作: %

hive> select 41 % 5 from lxw_dual;

1

hive> select 8.4 % 4 from lxw_dual;

0.40000000000000036

hive> select round(8.4 % 4 , 2) from lxw_dual;

0.4

6. 位与操作: &

hive> select 4 & 8 from lxw_dual;

0

hive> select 6 & 4 from lxw_dual;

4

7. 位或操作: |

hive> select 4 | 8 from lxw_dual;

12

hive> select 6 | 8 from lxw_dual;

14

8. 位异或操作: ^

hive> select 4 ^ 8 from lxw_dual;

12

hive> select 6 ^ 4 from lxw_dual;

2

9．位取反操作: ~

hive> select ~6 from lxw_dual;

-7

hive> select ~4 from lxw_dual;

-5

三、逻辑运算：

1. 逻辑与操作: AND

hive> select 1 from lxw_dual where 1=1 and 2=2;

1

2. 逻辑或操作: OR

hive> select 1 from lxw_dual where 1=2 or 2=2;

1

3. 逻辑非操作: NOT

hive> select 1 from lxw_dual where not 1=2;

1

四、数值计算

1. 取整函数: round

hive> select round(3.1415926) from lxw_dual;

3

hive> select round(3.5) from lxw_dual;

4

hive> create table lxw_dual as select round(9542.158) fromlxw_dual;

hive> describe lxw_dual;

_c0     bigint

2. 指定精度取整函数: round

hive> select round(3.1415926,4) from lxw_dual;

3.1416

3. 向下取整函数: floor

hive> select floor(3.1415926) from lxw_dual;

3

hive> select floor(25) from lxw_dual;

25

4. 向上取整函数: ceil

hive> select ceil(3.1415926) from lxw_dual;

4

hive> select ceil(46) from lxw_dual;

46

5. 向上取整函数: ceiling

hive> select ceiling(3.1415926) from lxw_dual;

4

hive> select ceiling(46) from lxw_dual;

46

6. 取随机数函数: rand

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

hive> select exp(2) from lxw_dual;

7.38905609893065

hive> select ln(7.38905609893065) from lxw_dual;

2.0

8. 以10为底对数函数: log10

hive> select log10(100) from lxw_dual;

2.0

9. 以2为底对数函数: log2

hive> select log2(8) from lxw_dual;

3.0

10. 对数函数: log

hive> select log(4,256) from lxw_dual;

4.0

11. 幂运算函数: pow

hive> select pow(2,4) from lxw_dual;

16.0

12. 幂运算函数: power

hive> select power(2,4) from lxw_dual;

16.0

13. 开平方函数: sqrt

hive> select sqrt(16) from lxw_dual;

4.0

14. 二进制函数: bin

hive> select bin(7) from lxw_dual;

111

15. 十六进制函数: hex

hive> select hex(17) from lxw_dual;

11

hive> select hex(‘abc’) from lxw_dual;

616263

16. 反转十六进制函数: unhex

hive> select unhex(‘616263’) from lxw_dual;

abc

hive> select unhex(‘11’) from lxw_dual;

-

hive> select unhex(616263) from lxw_dual;

abc

17. 进制转换函数: conv

hive> select conv(17,10,16) from lxw_dual;

11

hive> select conv(17,10,2) from lxw_dual;

10001

18. 绝对值函数: abs

hive> select abs(-3.9) from lxw_dual;

3.9

hive> select abs(10.9) from lxw_dual;

10.9

19. 正取余函数: pmod

hive> select pmod(9,4) from lxw_dual;

1

hive> select pmod(-9,4) from lxw_dual;

3

20. 正弦函数: sin

hive> select sin(0.8) from lxw_dual;

0.7173560908995228

21. 反正弦函数: asin

hive> select asin(0.7173560908995228) from lxw_dual;

0.8

22. 余弦函数: cos

hive> select cos(0.9) from lxw_dual;

0.6216099682706644

23. 反余弦函数: acos

hive> select acos(0.6216099682706644) from lxw_dual;

0.9

24. positive函数: positive

hive> select positive(-10) from lxw_dual;

-10

hive> select positive(12) from lxw_dual;

12

25. negative函数: negative

hive> select negative(-5) from lxw_dual;

5

hive> select negative(8) from lxw_dual;

-8

五、日期函数

1. UNIX时间戳转日期函数:from_unixtime

hive> select from_unixtime(1323308943,'yyyyMMdd') fromlxw_dual;

20111208

2. 获取当前UNIX时间戳函数:unix_timestamp

hive> select unix_timestamp() from lxw_dual;

1323309615

3. 日期转UNIX时间戳函数:unix_timestamp

hive> select unix_timestamp('2011-12-07 13:01:03') from lxw_dual;

1323234063

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

hive> select unix_timestamp('20111207 13:01:03','yyyyMMddHH:mm:ss') from lxw_dual;

1323234063

5. 日期时间转日期函数:to_date

hive> select to_date('2011-12-08 10:03:01') from lxw_dual;

2011-12-08

6. 日期转年函数: year

hive> select year('2011-12-08 10:03:01') from lxw_dual;

2011

hive> select year('2012-12-08') from lxw_dual;

2012

7. 日期转月函数: month

hive> select month('2011-12-08 10:03:01') from lxw_dual;

12

hive> select month('2011-08-08') from lxw_dual;

8

8. 日期转天函数: day

hive> select day('2011-12-08 10:03:01') from lxw_dual;

8

hive> select day('2011-12-24') from lxw_dual;

24

9. 日期转小时函数: hour

hive> select hour('2011-12-08 10:03:01') from lxw_dual;

10

10. 日期转分钟函数: minute

hive> select minute('2011-12-08 10:03:01') from lxw_dual;

3

11. 日期转秒函数: second

hive> select second('2011-12-08 10:03:01') from lxw_dual;

1

12. 日期转周函数:weekofyear

hive> select weekofyear('2011-12-08 10:03:01') from lxw_dual;

49

13. 日期比较函数: datediff

hive> select datediff('2012-12-08','2012-05-09') from lxw_dual;

213

2012-12-18

15. 日期减少函数: date_sub

hive> select date_sub('2012-12-08',10) from lxw_dual;

2012-11-28

六、条件函数

1. If函数: if

hive> select if(1=2,100,200) from lxw_dual;

200

hive> select if(1=1,100,200) from lxw_dual;

100

2. 非空查找函数: COALESCE

hive> select COALESCE(null,'100','50) from lxw_dual;

100

3. 条件判断函数：CASE

hive> Select case 100 when 50 then 'tom' when 100 then 'mary'else 'tim' end from lxw_dual;

mary

hive> Select case 200 when 50 then 'tom' when 100 then 'mary'else 'tim' end from lxw_dual;

tim

4. 条件判断函数：CASE

hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else'tim' end from lxw_dual;

mary

hive> select case when 1=1 then 'tom' when 2=2 then 'mary' else'tim' end from lxw_dual;

tom

七、字符串函数

1. 字符串长度函数：length

hive> select length('abcedfg') from lxw_dual;

7

2. 字符串反转函数：reverse

hive> select reverse(abcedfg’) from lxw_dual;

gfdecba

3. 字符串连接函数：concat

hive> select concat(‘abc’,'def’,'gh’) from lxw_dual;

abcdefgh

4. 带分隔符字符串连接函数：concat_ws

hive> select concat_ws(',','abc','def','gh') from lxw_dual;

abc,def,gh

5. 字符串截取函数：substr,substring

hive> select substr('abcde',3) from lxw_dual;

cde

hive> select substring('abcde',3) from lxw_dual;

cde

hive>  selectsubstr('abcde',-1) from lxw_dual;  （和ORACLE相同）

e

6. 字符串截取函数：substr,substring

hive> select substr('abcde',3,2) from lxw_dual;

cd

hive> select substring('abcde',3,2) from lxw_dual;

cd

hive>select substring('abcde',-2,2) from lxw_dual;

de

7. 字符串转大写函数：upper,ucase

hive> select upper('abSEd') from lxw_dual;

hive> select ucase('abSEd') from lxw_dual;

8. 字符串转小写函数：lower,lcase

hive> select lower('abSEd') from lxw_dual;

absed

hive> select lcase('abSEd') from lxw_dual;

absed

9. 去空格函数：trim

hive> select trim(' abc ') from lxw_dual;

abc

10. 左边去空格函数：ltrim

hive> select ltrim(' abc ') from lxw_dual;

abc

11. 右边去空格函数：rtrim

hive> select rtrim(' abc ') from lxw_dual;

abc

12. 正则表达式替换函数：regexp_replace

hive> select regexp_replace('foobar', 'oo|ar', '') from lxw_dual;

fb

13. 正则表达式解析函数：regexp_extract

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) fromlxw_dual;

the

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) fromlxw_dual;

bar

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) fromlxw_dual;

foothebar

select data_field,

regexp_extract(data_field,'.*?bgStart\\=([^&]+)',1) as aaa,

where pt = '2012-03-26'limit 2;

v1

15. json解析函数：get_json_object

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

hive> select space(10) from lxw_dual;

hive> select length(space(10)) from lxw_dual;

10

17. 重复字符串函数：repeat

hive> select repeat('abc',5) from lxw_dual;

abcabcabcabcabc

18. 首字符ascii函数：ascii

hive> select ascii('abcde') from lxw_dual;

97

tdtdtdtabc

abctdtdtdt

21. 分割字符串函数: split

hive> select split('abtcdtef','t') from lxw_dual;

["ab","cd","ef"]

22. 集合查找函数:find_in_set

hive> select find_in_set('ab','ef,ab,de') from lxw_dual;

2

hive> select find_in_set('at','ef,ab,de') from lxw_dual;

0

八、集合统计函数

1. 个数统计函数: count

count(*)    所有值不全为NULL时，加1操作

count(1)    不管有没有值，只要有这条记录，值就加1

count(col)  col列里面的值为null，值不会加1，这个列里面的值不为NULL，才加1

hive> select count(*) from lxw_dual;

20

hive> select count(distinct t) from lxw_dual;

10

2. 总和统计函数: sum

hive> select sum(t) from lxw_dual;

100

hive> select sum(distinct t) from lxw_dual;

70

3. 平均值统计函数: avg

hive> select avg(t) from lxw_dual;

50

hive> select avg (distinct t) from lxw_dual;

30

4. 最小值统计函数: min

hive> select min(t) from lxw_dual;

20

5. 最大值统计函数: max

hive> select max(t) from lxw_dual;

120

11. 中位数函数:percentile

select percentile(score,<0.2,0.4>) from lxw_dual 0.20.4位置的数据

14. 直方图:histogram_numeric

hive> select histogram_numeric(100,5) from lxw_dual;

[{"x":100.0,"y":1.0}]

九、复合类型构建操作

1. Map类型构建: map

hive> Create table lxw_test as select map('100','tom','200','mary')as t from lxw_dual;

hive> describe lxw_test;

t       map<string,string>

hive> select t from lxw_test;

{"100":"tom","200":"mary"}

2. Struct类型构建: struct

hive> create table lxw_test as select struct('tom','mary','tim')as t from lxw_dual;

hive> describe lxw_test;

t       struct<col1:string,col2:string,col3:string>

hive> select t from lxw_test;

{"col1":"tom","col2":"mary","col3":"tim"}

3. array类型构建: array

hive> create table lxw_test as selectarray("tom","mary","tim") as t from lxw_dual;

hive> describe lxw_test;

t       array<string>

hive> select t from lxw_test;

["tom","mary","tim"]

十、复杂类型访问操作

1. array类型访问: A[n]

hive> create table lxw_test as selectarray("tom","mary","tim") as t from lxw_dual;

hive> select t[0],t[1],t[2] from lxw_test;

tom     mary    tim

2. map类型访问: M[key]

hive> Create table lxw_test as selectmap('100','tom','200','mary') as t from lxw_dual;

hive> select t['200'],t['100'] from lxw_test;

mary    tom

3. struct类型访问: S.x

hive> create table lxw_test as select struct('tom','mary','tim')as t from lxw_dual;

hive> describe lxw_test;

t       struct<col1:string,col2:string,col3:string>

hive> select t.col1,t.col3 from lxw_test;

tom     tim

十一、复杂类型长度统计函数

1.    Map类型长度函数: size(Map<K.V>)

hive> select size(map('100','tom','101','mary')) from lxw_dual;

2

2.    array类型长度函数: size(Array<T>)

hive> select size(array('100','101','102','103')) from lxw_dual;

4

3.    类型转换函数

hive> select cast(1 as bigint) from lxw_dual;

1

浮云飘飘

Hive 内建操作符与函数开发——深入浅出学Hive

2014/01/15
335
0
hive 自带 function

LIKE比较: LIKE 语法: A LIKE B 操作类型: strings 描述: 如果字符串A或者字符串B为NULL，则返回NULL；如果字符串A符合表达式B的正则语法，则为TRUE；否则为FALSE。B中字符”_”表示任意单个...

xiaozhou18
2016/11/29
34
0
Hive从入门到实战视频教程【40讲全】

tom先生
2014/11/26
48
0

2016/04/26
72
0
Hive自定义函数与JDBC连接

2018/05/22
0
0

dreamness
21分钟前
1
0

27分钟前
1
0
NCRE考试感想 三级信息安全(上)

34分钟前
1
0

39分钟前
1
0
mysql mysql常用的常用函数

1. 数学函数 函 数 作 用 ABS(x) 返回x的绝对值 CEIL(x),CEILIN(x) 返回不小于x的最小整数值 FLOOR(x) 返回不大于x的最大整数值 RAND() 返回0~1的随机数 RAND(x) 返回0~1的随机数，x值相同返...

edison_kwok

2
0