• 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,数据添加成功。

      

      

  • 相关阅读:
    spring aop实现数据库的读写分离
    MySQL主从复制(Master-Slave)实践
    java 注解 Annontation
    java NIO介绍
    为什么你学不会递归?告别递归,谈谈我的一些经验(转)
    maven的安装、路径配置、修改库文件路径和eclipse中的配置、创建maven工程(转)
    Eclipse中创建Maven多模块工程
    Eclipse的Working Set管理项目
    Java使用POI读取和写入Excel指南(转)
    Webpack安装和命令
  • 原文地址:https://www.cnblogs.com/invban/p/5331159.html
Copyright © 2020-2023  润新知