hive函数汇总

1、Hive Shell参数

1、Hive命令行

  • hive [-hiveconf x=y]* [<-i filename>]* [<-f filename>|<-e query-string>] [-S]

说明:

  1. -i 从文件初始化HQL。
  2. -e从命令行执行指定的HQL 
  3. -f 执行HQL脚本
  4. -v 输出执行的HQL语句到控制台
  5. -p <port> connect to Hive Server on port number
  6. -hiveconf x=y Use this to set hive/hadoop configuration variables.  设置hive运行时候的参数配置

2、Hive参数配置方式

Hive参数大全:

https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties

开发Hive应用时,不可避免地需要设定Hive的参数。设定Hive的参数可以调优HQL代码的执行效率,或帮助定位问题。然而实践中经常遇到的一个问题是,为什么设定的参数没有起作用?这通常是错误的设定方式导致的。

 

对于一般参数,有以下三种设定方式:

  1. 配置文件  hive-site.xml
  2. 命令行参数  启动hive客户端的时候可以设置参数
  3. 参数声明   进入客户单以后设置的一些参数  set  

配置文件:Hive的配置文件包括

  1. 用户自定义配置文件:$HIVE_CONF_DIR/hive-site.xml
  2. 默认配置文件:$HIVE_CONF_DIR/hive-default.xml

用户自定义配置会覆盖默认配置。

另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hive的配置会覆盖Hadoop的配置。

配置文件的设定对本机启动的所有Hive进程都有效。

 

命令行参数:启动Hive(客户端或Server方式)时,可以在命令行添加-hiveconf param=value来设定参数,例如:

bin/hive -hiveconf hive.root.logger=INFO,console

这一设定对本次启动的Session(对于Server方式启动,则是所有请求的Sessions)有效。

参数声明:可以在HQL中使用SET关键字设定参数,例如:

set mapred.reduce.tasks=100;

这一设定的作用域也是session级的。

上述三种设定方式的优先级依次递增。即参数声明覆盖命令行参数,命令行参数覆盖配置文件设定。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在Session建立以前已经完成了。

参数声明  >   命令行参数   >  配置文件参数(hive)

 

3、使用变量传递参数

实际工作当中,我们一般都是将hive的hql语法开发完成之后,就写入到一个脚本里面去,然后定时的通过命令 hive  -f  去执行hive的语法即可,然后通过定义变量来传递参数到hive的脚本当中去,那么我们接下来就来看看如何使用hive来传递参数。

hive0.9以及之前的版本是不支持传参的
hive1.0版本之后支持  hive -f 传递参数

在hive当中我们一般可以使用hivevar或者hiveconf来进行参数的传递

hiveconf使用说明

hiveconf用于定义HIVE执行上下文的属性(配置参数),可覆盖覆盖hive-site.xml(hive-default.xml)中的参数值,如用户执行目录、日志打印级别、执行队列等。例如我们可以使用hiveconf来覆盖我们的hive属性配置,

hiveconf变量取值必须要使用hiveconf作为前缀参数,具体格式如下:

      ${hiveconf:key} 

bin/hive --hiveconf "mapred.job.queue.name=root.default"

hivevar使用说明

hivevar用于定义HIVE运行时的变量替换,类似于JAVA中的“PreparedStatement”,与“${key}”配合使用或者与 ${hivevar:key}

对于hivevar取值可以不使用前缀hivevar,具体格式如下:

使用前缀:

       ${hivevar:key}

不使用前缀:

       ${key}

define使用说明

define与hivevar用途完全一样,还有一种简写“-d

hive --hiveconf "mapred.job.queue.name=root.default" -d my="201809" --database mydb

#   执行SQL

select * from mydb where concat(year, month) = ${my} limit 10;

hiveconf与hivevar使用实战

需求:hive当中执行以下hql语句,并将参数全部都传递进去

select * from student left join score on student.s_id = score.s_id where score.month = '201806' and score.s_score > 80 and score.c_id = 03;

第一步:定义hive脚本

开发hql脚本,并使用hiveconf和hivevar进行参数穿肚

node03执行以下命令定义hql脚本

cd /export/servers/hivedatas

vim hivevariable.hql

 

use myhive;

select * from student left join score on student.s_id = score.s_id where score.month = ${hiveconf:month} an

d score.s_score > ${hivevar:s_score} and score.c_id = ${c_id};

第二步:调用hive脚本传递参数

node03执行以下命令并

[root@node03 hive-1.1.0-cdh5.14.0]# bin/hive --hiveconf month=201806 --hivevar s_score=80 --hivevar c_id=03  -f /export/servers/hivedatas/hivevariable.hql

 

2、Hive函数介绍以及内置函数查看

 

 

内容较多,见《Hive官方文档》

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

 

1)查看系统自带的函数

hive> show functions;

2)显示自带的函数的用法

hive> desc function upper;

3)详细显示自带的函数的用法

hive> desc function extended upper;

3常用函数介绍

关系运算

1、等值比较: =

语法:A=B
操作类型:所有基本类型
描述: 如果表达式A与表达式B相等,则为TRUE;否则为FALSE

hive> select 1 from tableName where 1=1;

 

2、不等值比较: <>

语法: A <> B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A与表达式B不相等,则为TRUE;否则为FALSE

hive> select 1 from tableName where 1 <> 2;

 

3、小于比较: <

语法: A < B
操作类型:所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于表达式B,则为TRUE;否则为FALSE

hive> select 1 from tableName where 1 < 2;

 

4、小于等于比较: <=

语法: A <= B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于或者等于表达式B,则为TRUE;否则为FALSE

hive> select 1 from tableName where 1 < = 1;

 

5、大于比较: >

语法: A > B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A大于表达式B,则为TRUE;否则为FALSE

hive> select 1 from tableName where 2 > 1;

 

6、大于等于比较: >=

语法: A >= B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A大于或者等于表达式B,则为TRUE;否则为FALSE

hive> select 1 from tableName where 1 >= 1;

1

注意:String的比较要注意(常用的时间比较可以先 to_date 之后再比较)

hive> select * from tableName;

OK

2011111209 00:00:00     2011111209

 

hive> select a, b, a<b, a>b, a=b from tableName;

2011111209 00:00:00     2011111209      false   true    false

7、空值判断: IS NULL

语法: A IS NULL
操作类型: 所有类型
描述: 如果表达式A的值为NULL,则为TRUE;否则为FALSE

hive> select 1 from tableName where null is null;

 

8、非空判断: IS NOT NULL

语法: A IS NOT NULL
操作类型: 所有类型
描述: 如果表达式A的值为NULL,则为FALSE;否则为TRUE

hive> select 1 from tableName where 1 is not null;

 

9、LIKE比较: LIKE

语法: A LIKE B
操作类型: strings
描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合表达式B 的正则语法,则为TRUE;否则为FALSE。B中字符”_”表示任意单个字符,而字符”%”表示任意数量的字符。

hive> select 1 from tableName where 'football' like 'foot%';

 

hive> select 1 from tableName where 'football' like 'foot____';

 

<strong>注意:否定比较时候用NOT A LIKE B</strong>

hive> select 1 from tableName where NOT 'football' like 'fff%';

 

10、JAVA的LIKE操作: RLIKE

语法: A RLIKE B
操作类型: strings
描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE;否则为FALSE。

hive> select 1 from tableName where 'footbar' rlike '^f.*r$';

1

注意:判断一个字符串是否全为数字:

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

1

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

11、REGEXP操作: REGEXP

语法: A REGEXP B
操作类型: strings
描述: 功能与RLIKE相同

hive> select 1 from tableName where 'footbar' REGEXP '^f.*r$';

1

数学运算:

1、加法操作: +

语法: A + B
操作类型:所有数值类型
说明:返回A与B相加的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。比如,int + int 一般结果为int类型,而 int + double 一般结果为double类型

hive> select 1 + 9 from tableName;

10

hive> create table tableName as select 1 + 1.2 from tableName;

hive> describe tableName;

_c0     double

2、减法操作: -

语法: A – B
操作类型:所有数值类型
说明:返回A与B相减的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。比如,int – int 一般结果为int类型,而 int – double 一般结果为double类型

hive> select 10 – 5 from tableName;

5

hive> create table tableName as select 5.6 – 4 from tableName;

hive> describe tableName;

_c0     double

3、乘法操作: *

语法: A * B
操作类型:所有数值类型
说明:返回A与B相乘的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。注意,如果A乘以B的结果超过默认结果类型的数值范围,则需要通过cast将结果转换成范围更大的数值类型

hive> select 40 * 5 from tableName;

200

4、除法操作: /

语法: A / B
操作类型:所有数值类型
说明:返回A除以B的结果。结果的数值类型为double

hive> select 40 / 5 from tableName;

8.0

注意:hive中最高精度的数据类型是double,只精确到小数点后16位,在做除法运算的时候要特别注意

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

结果为4

hive>select ceil(28.0/6.99999999999999) from tableName limit 1;          

结果为5

5、取余操作: %

语法: A % B
操作类型:所有数值类型
说明:返回A除以B的余数。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 41 % 5 from tableName;

1

hive> select 8.4 % 4 from tableName;

0.40000000000000036

<strong>注意</strong>:精度在hive中是个很大的问题,类似这样的操作最好通过round指定精度

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

0.4

6、位与操作: &

语法: A & B
操作类型:所有数值类型
说明:返回A和B按位进行与操作的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 4 & 8 from tableName;

0

hive> select 6 & 4 from tableName;

4

7、位或操作: |

语法: A | B
操作类型:所有数值类型
说明:返回A和B按位进行或操作的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 4 | 8 from tableName;

12

hive> select 6 | 8 from tableName;

14

8、位异或操作: ^

语法: A ^ B
操作类型:所有数值类型
说明:返回A和B按位进行异或操作的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 4 ^ 8 from tableName;

12

hive> select 6 ^ 4 from tableName;

2

9.位取反操作: ~

语法: ~A
操作类型:所有数值类型
说明:返回A按位取反操作的结果。结果的数值类型等于A的类型。

hive> select ~6 from tableName;

-7

hive> select ~4 from tableName;

-5

逻辑运算:

1、逻辑与操作: AND

语法: A AND B
操作类型:boolean
说明:如果A和B均为TRUE,则为TRUE;否则为FALSE。如果A为NULL或B为NULL,则为NULL

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

1

2、逻辑或操作: OR

语法: A OR B
操作类型:boolean
说明:如果A为TRUE,或者B为TRUE,或者A和B均为TRUE,则为TRUE;否则为FALSE

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

1

3、逻辑非操作: NOT

语法: NOT A
操作类型:boolean
说明:如果A为FALSE,或者A为NULL,则为TRUE;否则为FALSE

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

1

数值计算

1、取整函数: round  ***

语法: round(double a)
返回值: BIGINT
说明: 返回double类型的整数值部分 (遵循四舍五入)

hive> select round(3.1415926) from tableName;

3

hive> select round(3.5) from tableName;

4

hive> create table tableName as select round(9542.158) from tableName;

hive> describe tableName;

_c0     bigint

2、指定精度取整函数: round  ***

语法: round(double a, int d)
返回值: DOUBLE
说明: 返回指定精度d的double类型

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

3.1416

3、向下取整函数: floor  ***

语法: floor(double a)
返回值: BIGINT
说明: 返回等于或者小于该double变量的最大的整数

hive> select floor(3.1415926) from tableName;

3

hive> select floor(25) from tableName;

25

4、向上取整函数: ceil ***

语法: ceil(double a)
返回值: BIGINT
说明: 返回等于或者大于该double变量的最小的整数

hive> select ceil(3.1415926) from tableName;

4

hive> select ceil(46) from tableName;

46

5、向上取整函数: ceiling ***

语法: ceiling(double a)
返回值: BIGINT
说明: 与ceil功能相同

hive> select ceiling(3.1415926) from tableName;

4

hive> select ceiling(46) from tableName;

46

6、取随机数函数: rand ***

语法: rand(),rand(int seed)
返回值: double
说明: 返回一个0到1范围内的随机数。如果指定种子seed,则会等到一个稳定的随机数序列

hive> select rand() from tableName;

0.5577432776034763

hive> select rand() from tableName;

0.6638336467363424

hive> select rand(100) from tableName;

0.7220096548596434

hive> select rand(100) from tableName;

0.7220096548596434

7、自然指数函数: exp

语法

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值