zoukankan      html  css  js  c++  java
  • Hive入门之UDFS函数

    一.UDFS函数介绍

    1. 基本UDF

    (1)SHOWFUNCTIONS:这个用来熟悉未知函数。

         DESCRIBE FUNCTION<function_name>;

    (2)A IS NULL

         A IS NOT NULL

    (3)A LIKE B 普通sql匹配如 like “a%”

         A RLIKE B通过正则表达式匹配

         A REGEXP B 通过正则表达式匹配

    (4)round(double a):四舍五入

    (5)rand(),rand(int seed):返回在(0,1)平均分布的随机数

    (6)COALESCE(pv, 0):将 pv 为 null 的行转为0,很实用

    2. 日期函数

    (1)datediff(string enddate, stringstartdate):

         返回enddate和startdate的天数的差,例如datediff('2009-03-01','2009-02-27') = 2

    (2)date_add(stringstartdate, int days):

         加days天数到startdate:date_add('2008-12-31', 1) ='2009-01-01'

    (3)date_sub(stringstartdate, int days):

         减days天数到startdate:date_sub('2008-12-31', 1) ='2008-12-30'

    (4)date_format(date,date_pattern)

         CREATETEMPORARY FUNCTION date_format AS'com.taobao.hive.udf.UDFDateFormat';

         根据格式串format 格式化日期和时间值date,返回结果串。

         date_format('2010-10-10','yyyy-MM-dd','yyyyMMdd')

    (5)str_to_date(str,format)

         将字符串转化为日期函数

    3. 字符串函数

    (1)length(stringA):返回字符串长度

    (2)concat(stringA, string B...):

         合并字符串,例如concat('foo','bar')='foobar'。注意这一函数可以接受任意个数的参数

    (3)substr(stringA, int start) substring(string A,int start):

         返回子串,例如substr('foobar',4)='bar'

    (4)substring(string A, int start,int len):

         返回限定长度的子串,例如substr('foobar',4, 1)='b'

    (5)split(stringstr, string pat):

         返回使用pat作为正则表达式分割str字符串的列表。例如,split('foobar','o')[2] = 'bar'。

    (6)getkeyvalue(str,param):

         从字符串中获得指定 key 的 value 值 UDFKeyValue

    4. 自定义函数

    (1)row_number

        create temporary function row_number as 'function_name';

        select ip,uid,row_number(ip,uid) from (

        select ip,uid,logtime from atpanel

        distribute by ip,uid

        sort by ip,uid,logtime desc) a  


    (2)拆分key_value键值对

      CREATE TEMPORARY FUNCTION ExplodeEX AS 'com.taobao.hive.udtf.UDTFExplodeEX';  

      select

      split(kvs,'_')[0] as key,

      split(kvs,'_')[1] as key,

      from ( select 'a-1|b-2' as kv from dual ) t 

      lateral view explode (split(kv,'\|')) result as kvs  

  • 相关阅读:
    SPOJ LCS2
    SPOJ NSUBSTR
    1977: [BeiJing2010组队]次小生成树 Tree
    2002: [Hnoi2010]Bounce 弹飞绵羊
    P3690 【模板】Link Cut Tree (动态树)
    P2093 [国家集训队]JZPFAR
    2648: SJY摆棋子
    HDU 2966 In case of failure
    bzoj 一些题目汇总
    BZOJ3653谈笑风生——可持久化线段树+dfs序
  • 原文地址:https://www.cnblogs.com/sunfie/p/4374837.html
Copyright © 2011-2022 走看看