zoukankan      html  css  js  c++  java
  • hive函数之~reflect函数

    reflect函数可以支持在sql中调用java中的自带函数,秒杀一切udf函数。

    使用java.lang.Math当中的Max求两列中最大值

    创建hive表

    create table test_udf(col1 int,col2 int) row format delimited fields terminated by ',';

    准备数据并加载数据

    cd /export/servers/hivedatas
    vim test_udf

    1,2

    4,3

    6,4

    7,5

    5,6

    加载数据

    hive (hive_explode)> load data local inpath '/export/servers/hivedatas/test_udf' overwrite into table test_udf;

    使用java.lang.Math当中的Max求两列当中的最大值

    hive (hive_explode)> select reflect("java.lang.Math","max",col1,col2) from test_udf;

    不同记录执行不同的java内置函数

    创建hive表

    hive (hive_explode)> create table test_udf2(class_name string,method_name string,col1 int , col2 int) row format delimited fields terminated by ',';

    准备数据

    cd /export/servers/hivedatas
    vim test_udf2

    java.lang.Math,min,1,2

    java.lang.Math,max,2,3

    加载数据

    hive (hive_explode)> load data local inpath '/export/servers/hivedatas/test_udf2' overwrite into table test_udf2;

    执行查询

    hive (hive_explode)> select reflect(class_name,method_name,col1,col2) from test_udf2;

    判断是否为数字

    使用apache commons中的函数,commons下的jar已经包含在hadoop的classpath中,所以可以直接使用。

    使用方式如下:

    select reflect("org.apache.commons.lang.math.NumberUtils","isNumber","123")
  • 相关阅读:
    1-29反射
    1-28Map简介
    1-27TreeSet简介
    1-26HashSet简介
    1-25泛型
    1-24List三个子类的特点
    1-23集合概述
    Java接口
    1-22日期类型
    简易计算器的实现
  • 原文地址:https://www.cnblogs.com/lojun/p/13251427.html
Copyright © 2011-2022 走看看