记录 hive 三类函数:取整函数、随机抽样(rand()函数)、数组蕴含函数(array_contains)、later view explode函数

取整函数

  1. round(A,B) :
    四舍五入,A是整型或者浮点型参数,B是要保留的小数位数,B是可选参数,不写的话默认对浮点数保留一位小数

    select round(-1.23,1)> -1.2select round(2.546,2)> 2.55select round(2.34)> 2.3
  2. floor(A):取坐标轴上的左值

    select floor(-1.3) > -2select floor(1.3) > 1
  3. ceil(A) :取坐标轴上的右值 ,select floor(-1.3)

    select ceil(-1.3) > -1select ceil(1.3) > 2

    抽样函数(rand())

    在大规模数据量的数据集上有两种应用形式,能够随机抽取小数据量的数据进行数据分析和建模:

  4. order by rand() 是对全局的数据做排序,会把所有数据放进一个reducer中解决,如果数据量十分大,可能会比拟耗时:

    select * from table_name where col=xxx order by rand() limit num;
  5. 在rand() 前指定 distribute 和 sort 关键字能够保证数据在mapper和reduce阶段都是随机的,distribute by是管制在map端如何拆分数据给reduce端的,sort by是部分排序,会在每个reduce端做排序,实用于数据量较大的状况:

    select * from table_name where col=xxx distribute by rand() sort by rand() limit num;

    数组蕴含函数,用于判断数组中是否蕴含某个值,返回true 或 false

    array_contains(数组,值),返回布尔类型值

    select *from dulux_dataset_101  where array_contains(mark,' 10')limit 10

later view expload

explode() 是UDTF 函数的一种,能够将一行数据依照分隔符变成多行,然而不能和其它列一起查出,例如dulux_dataset_101中mark字段是个数组

selectexplode(mark) as mark_explodefrom dulux_dataset_101limit 10


这样就会间接报错,因为mainid能返回的行数和mark_explode不统一

select mianid,explode(mark) as mark_explodefrom dulux_dataset_101limit 10

于是要和 lateral view 搭配应用,temptable为虚构表的别名,mark_explode为切分后的字段名

select mainid,mark_explodefrom dulux_dataset_101 lateral view explode(mark) temptable as mark_explode where array_contains(mark,' 10')limit 10