[Hive_6] Hive 的内置函数应用


0. 说明

  Hive 的内置函数的基本操作 | 时间函数 | String 函数 | 条件语句 | explode | split | substring 

 


1. 基本操作

  查看函数

  show functions;

 

  查看函数的用法

    desc function function_name;

 

  查看函数的扩展信息

  desc function extended format_name;

 

 


 

2. 时间函数

 

select current_database()        //当前数据库
select current_date()            //当前日期
select current_timestamp()        //当前时间戳,精确到毫秒
select date_format( current_timestamp(), 'yyyy-MM-dd HH:mm:ss');    //将时间格式化
select unix_timestamp(current_timestamp());    //将日期转换成时间戳,精确到秒
select from_unixtime(153361440000, 'yyyy-MM-dd');    //将时间戳转化成日期
select datediff('2018-03-01','2018-02-01');        //计算两个指定日期相差多少天

 

 


 

3. String 函数

select split('hello world',' ');    // 以空格进行切割成 Array 数组
select substr('hello world', 7);    // 切割字符串 world

select trim(' world');        //去掉前后的空格

format_number()
//select format_number(1234.345,2); => 1,234.35
//select format_number(1234.345,'000000.00'); => 001234.35
//select format_number(1234.345,'########.##'); => 1234.35

select concat('hello', ' world');        // 拼串操作,返回 hello world

select length('helloworld');    // 10

 

 


 

4. 条件语句

  4.1 if

select if( age > 10 ,'old', 'young') from user_par;

  // 相当于三元运算符。
  // 第一个表达式成立返回第二个表达式
  // 第一个不成立返回第三个表达式

 

  4.2 case

select case when age<20 then 'young' when age<40 then 'middle' else 'old' end from user_par; 

  // 小于20,返回 young
  // 小于40,返回 middle
  // 否则,返回 old

 


 5. explode

  5.1 描述

  separates the elements of array a into multiple rows
  or the elements of a map into multiple rows and columns

  分裂 array 数组的中的元素变成多行
  或者分裂 map 中的元素变成多行和多列
  是一个表生成函数

  5.2 使用方法

  explode(array)

 

 


 

6. split

  6.1 描述

  Splits str around occurances that match regex

  拆分与正则表达式匹配的事件

 

  6.2 使用方法

  split(str, regex)

 

 


7. substr

  7.1 描述

  截取从指定位置开始指定长度的的字符串

  默认从第一位开始截取

 

  7.2 使用方法

  substr(str, pos[, len])

 

 

 


 

posted @ 2019-01-05 11:57  山间一棵松  阅读(233)  评论(0编辑  收藏  举报