Hadoop_UDF示例

时间:2024-04-21 11:59:13

UDF:  一进一出

Eclipse端
1. 继承UDF
2. 实现evaluate方法(可重裁实现多个evaluate方法,以实现不同需求)
3. 导出类jar包,注意指定main方法
Hive端
1. 将jar包添加到Hive: add jar linux_path # 0.14版才开始支持
2. 创建临时函数: create [temporary] function [if not exists] f_name classpath
删除临时函数: drop [temporary] function [if exists] f_name

简单示例,去除某一列双引号

package com.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text; public class RMQuotes extends UDF{   public Text evaluate(Text str){
if(str != null){
  return new Text(str.toString().replaceAll("\"", ""));
}else return null;
  }
  public static void main(String[] args) {
System.out.println(new RMQuotes().evaluate(new Text("\"hbhb\" \"GET /SSS/DDD/FFF?id=8 HTTP/1.1\"")));
  }
}