使用eclipse编写UDF函数

xiaoxiao2021-02-28  92

在做日志分析的过程中,用到了Hadoop框架中的Hive,不过有些日志处理用hive中的函数处理显得力不从心,就需要用udf来进行扩展处理了

1  在eclipse中新建Java project   hiveudf   然后新建class  package(com.afan)  name(UDFLower)

2  添加jar library  hadoop-0.20.2-core.jar   hive-exec-0.7.0-cdh3u0.jar两个文件到project

3  编写代码

   

package com.afan;   import org.apache.hadoop.hive.ql.exec.UDF;   import org.apache.hadoop.io.Text;      public class UDFLower extends UDF{       public Text evaluate(final Text s){           if (null == s){               return null;           }           return new Text(s.toString().toLowerCase());       }   }   4  编译输出打包文件为 udf_hive.jar

5 将udf_hive.jar放入配置好的Linux系统的文件夹中路径为/home/udf/udf_hive.jar

6 打开hive命令行测试

   hive> add jar /home/udf/udf_hive.jar;

创建udf函数 hive> create temporary function my_lower as 'com.afan.UDFLower';

使用udf函数 hive> select my_lower(info) from dual;

转载请注明原文地址: https://www.6miu.com/read-78526.html

最新回复(0)