2013-03-05 udf

       今天主要是一些学习的记录。

        因为后台日志文件里的时间是英文格式的,在进行分析时需要转换为普通的YYYY-MM-DD HH:MM:ss的格式,查阅了hive的官方文档。

发现hive自身有提供一些时间函数,仔细看了下,都不能满足需求。就开始看了下UDF,打算自己写一个日期转换函数。

    1  Creating Custom UDFs
First, you need to create a new class that extends UDF, with one or more methods named evaluate.

package com.example.hive.udf;


import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;


public final class Lower extends UDF {
  public Text evaluate(final Text s) {
    if (s == null) { return null; }
    return new Text(s.toString().toLowerCase());
  }
}
(Note that there's already a built-in function for this, it's just an easy example).


After compiling your code to a jar, you need to add this to the hive classpath. See the section below on deploying jars.


Once hive is started up with your jars in the classpath, the final step is to register your function:


create temporary function my_lower as 'com.example.hive.udf.Lower';
Now you can start using it:


hive> select my_lower(title), sum(freq) from titles group by my_lower(title);

============================这是官网示例,很好理解。

需要说明的是,这个类的evaluate方法可以重载,也支持可变参数传递,比如public String evaluate(String... strs)


写完后可以将源代码上传到hadoop环境,使用javac命令进行编译,jar -cvf命令打包。如果像我一样是在eclipse中的话,直接导出为jar包,放到hive安装目录的lib下。

在hive语句中,使用add jar jar包路径;即可以加载你自己的udf类。然后就可以按照官网中的方法创建临时的函数,然后在select语句中直接使用。使用完成后,记得用drop temporary function 方法名 来删除这个临时函数。

 如果这个jar包不想用了,我没有看到add jar对应的del jar,直接将jar包删除即可。

另外,如果不想每次在hive会话中都add 同一个jar包,可以修改hive-site.xml文件,加入以下属性行:

<property>
<name>hive.aux.jars.path</name> (除了hive默认加载的jar外,仍然需要加载的jar包)
<value>file:///自定义的jar包路径</value>
</property>

hive 还有一种自定义数据处理的方法,transform

2 transform

      查询数据时可以对整行数据使用python脚本处理,官方资料:https://cwiki.apache.org/confluence/display/Hive/GettingStarted#GettingStarted-MetadataStore

即使用自定义的mapreduce。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值