龙空技术网

Spark SQL 中调用Java的类

豆豆杭哲 92

前言:

而今同学们对“java调用类中类”大约比较重视,朋友们都需要了解一些“java调用类中类”的相关内容。那么小编同时在网络上网罗了一些对于“java调用类中类””的相关文章,希望姐妹们能喜欢,朋友们一起来了解一下吧!

最近有需求,需要针对URL 进行解码,就是 网页中有些URL是经过编码的,我们为了看到明文需要进行解码

reflect('java.net.URLDecoder', 'decode',url ,'UTF-8') as use_url

+----------------------------------------------------------------------------------+|function_desc                                                                     |+----------------------------------------------------------------------------------+|Function: reflect                                                                 ||Class: org.apache.spark.sql.catalyst.expressions.CallMethodViaReflection          ||Usage: reflect(class, method[, arg1[, arg2 ..]]) - Calls a method with reflection.|+----------------------------------------------------------------------------------+

当然 实现肯定是很简单的,就是调用Java中的 decode,但是之前都没有接触过,也不了解,所以记录下,spark sql中 有 reflect 这个函数,可以调用Java中的类的静态方法。

package java.net;。。。。。public class URLDecoder {    // The platform default encoding    static String dfltEncName = URLEncoder.dfltEncName;    /**     * Decodes a {@code x-www-form-urlencoded} string.     * The platform's default encoding is used to determine what characters     * are represented by any consecutive sequences of the form     * "<i>{@code %xy}</i>".     * @param s the {@code String} to decode     * @deprecated The resulting string may vary depending on the platform's     *          default encoding. Instead, use the decode(String,String) method     *          to specify the encoding.     * @return the newly decoded {@code String}     */    @Deprecated    public static String decode(String s) {        String str = null;        try {            str = decode(s, dfltEncName);        } catch (UnsupportedEncodingException e) {            // The system should always have the platform default        }        return str;    }。。。。。。。

之前还真不知道,以为调用 Java中某个类的方法,只能通过 udf 来实现,现在发现这个 ,感觉比写udf清爽多了。

记录一下,之后肯定还会继续使用其他的Java类

标签: #java调用类中类