hive-udaf开发实例1
Posted forrestxingyunfei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive-udaf开发实例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.udaf;
import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
public class Wm_concat extends UDAF
public static class myUDAFEval implements UDAFEvaluator
private PartialResult partial = new PartialResult();
public static class PartialResult
String result = "" ;
String delimiter = null;
@Override
public void init()
// TODO Auto-generated method stub
partial.result = "";
public boolean iterate(String value, String deli)
if (value == null || "null".equalsIgnoreCase(value))
return true;
if (partial.delimiter == null)
partial.delimiter = deli;
if (partial.result.length() > 0)
partial.result = partial.result.concat(partial.delimiter);
partial.result = partial.result.concat(value);
return true;
public PartialResult terminatePartial()
return partial;
public boolean merge(PartialResult other)
if (other == null)
return true;
if (partial.delimiter == null)
partial.delimiter = other.delimiter ;
partial.result = other.result ;
else
if(partial.result.length() > 0)
partial.result = partial.result.concat(partial.delimiter);
partial.result = partial.result.concat(other.result);
return true ;
public String terminate()
if (partial == null || partial.result.length() == 0)
return null;
return partial.result ;
3,导出jar包
4,hive中导入jar包
addjar /opt/beifeng/jars/hive_UDF.jar;
createtemporary function Wm_concat as 'com.ibeifeng.udf.GetCmsID ';
5,hive sql使用udf
select getcmsid(url) from info_tab group by id;
以上是关于hive-udaf开发实例1的主要内容,如果未能解决你的问题,请参考以下文章