zoukankan      html  css  js  c++  java
  • Hive学习之五 《Hive进阶—UDF操作案例》 详解

    hive—UDF操作

    udf的操作过程:

    HIVE会话中add 自定义函数的jar文件,然后创建function,继而使用函数。

    下面就以下面课题为例:

    课题:统计每个活动的PV和UV

    一、Java通过正则表达式,截取标题名称。

    以链接,截取标红的字符串。

    http://cms.yhd.com/sale/vtxqCLCzfto?tc=ad.0.0.17280-32881642.1&tp=1.1.36.9.1.LEffwdz-10-35RcM&ti=ZX8H

    为例。

    核心代码如下,

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.apache.hadoop.hive.ql.exec.UDF;
    
    public class GetCommentNameOrId extends UDF {
        public String evaluate(String url,String flag){
            String str = null;
            Pattern p = Pattern.compile(flag+"/[a-zA-Z0-9]+");
            Matcher m = p.matcher(url);
            if(m.find()){
                str = m.group(0).toLowerCase().split("/")[1];
            }
            return str;
        }
        
        public static void main(String[] args) {
            String url = "http://cms.yhd.com/sale/vtxqCLCzfto?tc=ad.0.0.17280-32881642.1&tp=1.1.36.9.1.LEffwdz-10-35RcM&ti=ZX8H";
            GetCommentNameOrId gs = new GetCommentNameOrId();
            System.out.println(gs.evaluate(url,"sale"));
        }
    }

     传参:

    url:http://cms.yhd.com/sale/vtxqCLCzfto?tc=ad.0.0.17280-32881642.1&tp=1.1.36.9.1.LEffwdz-10-35RcM&ti=ZX8H

    flag:sale

    最后得到的结果是 :vtxqCLCzfto

     二、UDF操作

      1、在rptest库中创建表

    create table rptest.rpt_sale_daily(
    huodong string,
    pv bigint,
    uv bigint) partitioned by (ds string,hour string);

      2、打jar包,并上传到制定的路径

      add jar /opt/litong/lib/hiveUDF.jar

      3、指定属性类,创建function

      create temporary function GetCommentNameOrId as 'com.litong.hive.udf.GetCommentNameOrId';

      4、添加数据到表rpt_sale_daily中 

    insert overwrite table rptest.rpt_sale_daily partition (ds='2015-08-28',hour='18')
    select GetCommentNameOrId(url,"sale") huodong,count(url) pv,count(distinct guid) uv from default.track_log a 
    where ds='2015-08-28' and hour='18'
    group by ds,GetCommentNameOrId(url,"sale");
    
    insert overwrite table rptest.rpt_sale_daily partition (ds='2015-08-28',hour='19')
    select GetCommentNameOrId(url,"sale") huodong,count(url) pv,count(distinct guid) uv from default.track_log a 
    where ds='2015-08-28' and hour='19'
    group by ds,GetCommentNameOrId(u

      5、检查数据是否插入成功

      

          

    OK,数据添加成功。

      

      

  • 相关阅读:
    T-SQL常用的函数
    webservice和wcf和web.api简单介绍
    c#索引器
    在eclipse中使用maven构建spring cloud微服务
    maven项目报错maven-resources-plugin:2.7 or one of its dependencies could not be resolved
    使用maven创建工程报错Could not resolve archetype org.apache.maven.archetype
    eclipse配置maven
    最新省市区json数据
    ORA-01461: can bind a LONG value only for insert into a LONG column
    js验证强密码 大小写字母数字字符四选三 且大于8位
  • 原文地址:https://www.cnblogs.com/invban/p/5331159.html
Copyright © 2011-2022 走看看