在写hsql(hive sql)的时候,我们可以利用hive自身的函数来处理很多的数据,但是有的时候内置函数没法满足我们的需求时就需要用到UDF(User-Defined Function),而有时我们的需求也很简单,在很多的jar包或者java自身的包中已经存在具体的实现逻辑或者方法。那么我们如何来调用这些已经存在且不需要自己写的函数和方法呢?

A java class and method often exists to handle the exact function a user would like to use in hive. Rather then having to write a wrapper UDF to call this method, the majority of these methods can be called using reflect udf. Reflect uses java reflection to instantiate and call methods of objects, it can also call static functions. The method must return a primative type or a type that hive knows how to serialize.
Apache hive reflect
大致含义: 通常存在一个 Java 类和方法来处理用户想要在 Hive 中使用的确切功能。不必编写包装 UDF 来调用此方法,大多数方法都可以使用反射 UDF 调用。Reflect 使用 Java 反射来实例化和调用对象的方法;它还可以调用静态函数。该方法必须返回原始类型或 Hive 知道如何序列化的类型。
SELECT reflect("java.lang.String", "valueOf", 1), reflect("java.lang.String", "isEmpty"), reflect("java.lang.Math", "max", 2, 3), reflect("java.lang.Math", "min", 2, 3), reflect("java.lang.Math", "round", 2.5), reflect("java.lang.Math", "exp", 1.0), reflect("java.lang.Math", "floor", 1.9) FROM src LIMIT 1; # 输出: 1 true 3 2 3 2.7182818284590455 1.0
再比如我们在解析URL的时候,java的已存在函数是java.net.URLDecoder.decode(),在hive中如何调用?
hive> select reflect('java.net.URLDecoder','decode',cur) from pv where day_id='2016-05-01';
注意: As of Hive 0.9.0, java_method() is a synonym for reflect(). See Misc. Functions in Hive Operators and UDFs.
参考地址: http://sishuok.com/forum/blogPost/list/6226.html