zoukankan      html  css  js  c++  java
  • Hive之函数与自定义函数

    系统自带的函数

    1)查看系统自带的函数
    	hive> show functions;
    2)显示自带的函数的用法
    	hive> desc function upper;
    3)详细显示自带的函数的用法
    	hive> desc function extended upper;
    

    自定义函数

    1)Hive 自带了一些函数,比如:max/min 等,但是数量有限,自己可以通过自定义 UDF来方便的扩展。
    2)当 Hive 提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数(UDF:user-defined function)。
    

    自定义函数的类别:

    (1)UDF(User-Defined-Function)
    	一进一出,接受一个参数,返回一个结果
    (2)UDAF(User-Defined Aggregation Function):聚集函数,多进一出;类似于:count/max/min
    (3)UDTF(User-Defined Table-Generating Functions):一进多出;如 lateral view explore()
    

    自定义函数的步骤:

    (1)继承 org.apache.hadoop.hive.ql.UDF
    (2)需要实现 evaluate 函数;evaluate 函数支持重载;
    (3)在 hive 的命令行窗口创建函数
    a)添加 jar
    	add jar linux_jar_path
    b)创建 function,
    	create [temporary] function [dbname.]function_name AS class_name;
    

    注意

    UDF必须要有返回类型,可以返回null,但是不能是void

    案例:

    1)创建一个 java 工程,并创建一个 lib 文件夹
    2)将 hive 的 jar 包解压后,将 apache-hive-1.2.1-binlib 文件下的 jar 包都拷贝到 java 工程中。
    3)创建一个类
    
    public class Lower extends UDF {
    	public String evaluate (final String s) {
    		if (s == null) {
    			return null;
    		}
    		return s.toString().toLowerCase();
    	}
    }
    
    4)打成 jar 包上传到服务器/opt/module/jars/udf.jar
    5)将 jar 包添加到 hive 的 classpath
    	hive (default)> add jar /opt/module/datas/udf.jar;
    6)创建临时函数与开发好的 java class 关联
    	hive (default)> create temporary function my_lower as "com.atguigu.hive.Lower";
    7)即可在 hql 中使用自定义的函数 strip
    	hive (default)> select ename, my_lower(ename) lowername from emp;
    
    
    

    本博客仅为博主学习总结,感谢各大网络平台的资料。蟹蟹!!

  • 相关阅读:
    03_ if 练习 _ little2big
    uva 11275 3D Triangles
    uva 12296 Pieces and Discs
    uvalive 3218 Find the Border
    uvalive 2797 Monster Trap
    uvalive 4992 Jungle Outpost
    uva 2218 Triathlon
    uvalive 3890 Most Distant Point from the Sea
    uvalive 4728 Squares
    uva 10256 The Great Divide
  • 原文地址:https://www.cnblogs.com/shaofeer/p/11154300.html
Copyright © 2011-2022 走看看