您的位置:首页 > 其它

hive的常用函数

2018-04-06 00:11 274 查看
类型转换函数:

cast('1' as bigint)

日期函数:

返回值类型:

String from_unixtime(bigint unixtime[, string format]) 例:from_unixtime(0)="1970-01-01 00:00:00"

bigint unix_timestamp() 获取当前的时间戳

bigint unix_timestamp(String date) 获得date表示的时间戳 

String to_date(string timestamp) 返回时间中的年月日,例如to_date("1970-01-01 00:00:00") = "1970-01-01" 

int year(string date) 返回指定时间的年份,范围在1000到9999,或为”零”日期的0。

int month(string date) 返回指定时间的月份,范围为1至12月,或0一个月的一部分,如’0000-00-00′或’2008-00-00′的日期。

int date_add(string startdate, int days) 给定时间,在此基础上加上指定的时间段

int date_sub(string startdate, int days)   给定时间,在此基础上减去指定的时间段

Sting       from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')

int         cast(from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') as int)

from_unixtime(unix_timestamp('20160902000000','yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss')

条件函数:

if(boolean condition,value1,value2) 判断是否满足条件,如果满足返回第一个值,如果不满足则返回第二个值
例:if(vu.company is null,"",trim(vu.company)) as company 

case a when b then c     当a=b时,返回c;当a=d时,返回e,否则返回f
  when d then e
  else f end
  
case when a then b       当值为a时返回b,当值为c时返回d。否则返回e
 c then d
 else e end
 
 
字符函数:

返回值类型:

String   regexp_replace('2016-03-04','-','') ==> 20160304

int length(String a)   返回字符串的长度

String   reverse(String a)  返回倒序字符串

String   concat(String a,String b)  连接多个字符串,合并为一个字符串,可以接受任意数量的输入字符串

String   concat_ws(String sep,String a,String b)  链接多个字符串,字符串之间以指定的分隔符分开

String   trim(String a) 删除字符串两端的空格,字符之间的空格保留
  
String   regexp_replace(string A, string B, string C)  字符串A中的B字符被C字符替代

String     parse_url(string urlString, string partToExtract [, string keyToExtract])   返回URL指定的部分。parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1′, ‘HOST’) 返回:’facebook.com’

String     get_json_object(string json_string, string path)  解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL
  说明:json_string:json数据(json数据的字段名称),path:要获取的json数据的key。 得出要从json数据中胡获得数据

  
---------------------------------------------   
  
  
拼接列:concat , concat_ws

横向拼接:例如两张小表查出数据塞到一张宽表中

纵向拼接:union all

行转列:concat_ws(',',collect_list(BANK))

1.建立一张hive表

create table t_person(
  id int,
  name string,
  likes  array<string>,
  address map<string,string>
)
partitioned by (sex int)
row format delimited
    fields  terminated  by  ','
collection  items  terminated by '_'
    map  keys  terminated by ':'
stored as textfile;

2.找到需要分析文本文件数据person.txt

1,zs,sing_play,city:beijing_street:xisanqi
2,ls,play_sing,city:shanghai_street:xierqi3.shiyong 

3.使用命令,将本地文本文件中的数据倒入hive表

   load data local inpath '/root/person.txt' into table t_person  partition (sex=0);

判断语句:

if(not lower(lmlc.phone) like '%null%' ,lmlc.sys_id, ms.sys_id) as sys_id

  
   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: