hive-udf开发实例1
Posted forrestxingyunfei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive-udf开发实例1相关的知识,希望对你有一定的参考价值。
1,pom配置:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.1.0-cdh5.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-cdh5.8.3</version>
</dependency>
2,java代码:
package com.ibeifeng.udf;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hadoop.hive.ql.exec.UDF;
public class GetCmsID extends UDF
/**
* @param args
*/
public String evaluate(String url)
String cmsid = null;
if (url==null || "".equals(url))
return cmsid;
Pattern pat = Pattern.compile("topicId=[0-9]+") ;
Matcher matcher = pat.matcher(url);
if (matcher.find())
cmsid=matcher.group().split("topicId=")[1];
return cmsid;
public String evaluate(String pattern,String url)
String cmsid = null;
if (url==null || "".equals(url))
return cmsid;
Pattern pat = Pattern.compile(pattern+"[0-9]+") ;
Matcher matcher = pat.matcher(url);
if (matcher.find())
cmsid=matcher.group().split(pattern)[1];
return cmsid;
public static void main(String[] args)
// TODO Auto-generated method stub
String url="http://www.taobao.com/cms/view.do?topicId=18980";
GetCmsID getCmsID = new GetCmsID();
System.out.println(getCmsID.evaluate(url)) ;
System.out.println(getCmsID.evaluate("cmsid=",url)) ;
3,导出jar包
4,hive中导入jar包
addjar /opt/beifeng/jars/hive_UDF.jar;
createtemporary function getcmsid as 'com.ibeifeng.udf.GetCmsID ';
5,hive sql使用udf
select getcmsid(url) from info_tab;
以上是关于hive-udf开发实例1的主要内容,如果未能解决你的问题,请参考以下文章