内置的两个聚合函数(UDAF)

collect_list():多行字符串拼接为一行
collect_set():多行字符串拼接为一行并去重
多行字符串拼接为一行并相邻去重UDAF:Concat()

concat_udaf.jar

package com.tcc.udaf;

import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;

public class Concat extends UDAF
{
public static class ConcatUDAFEvaluator
implements UDAFEvaluator
{
private PartialResult partial;

public void init()
{
this.partial = null;
}

public boolean iterate(String value, String deli)
{
if (value == null) {
return true;
}
if (this.partial == null) {
this.partial = new PartialResult();
this.partial.result = new String("");
if ((deli == null) || (deli.equals("")))
{
this.partial.delimiter = new String(",");
}
else
{
this.partial.delimiter = new String(deli);
}
}

if (this.partial.result.length() > 0)
{
this.partial.result = this.partial.result.concat(this.partial.delimiter);
}

this.partial.result = this.partial.result.concat(value);

return true;
}

public PartialResult terminatePartial() {
return this.partial;
}

public boolean merge(PartialResult other) {
if (other == null) {
return true;
}
if (this.partial == null) {
this.partial = new PartialResult();
this.partial.result = new String(other.result);
this.partial.delimiter = new String(other.delimiter);
}
else
{
if (this.partial.result.length() > 0)
{
this.partial.result = this.partial.result.concat(this.partial.delimiter);
}
this.partial.result = this.partial.result.concat(other.result);
}
return true;
}

public String terminate() {
String s = new String(this.partial.result);

if (s.indexOf(this.partial.delimiter) != -1) {
String[] str = s.split(this.partial.delimiter);

StringBuffer sb = new StringBuffer();

int i = 0; int j = 1;
while (i < str.length - 1) {
while (j < str.length) {
if (str[j].equals(str[i])) {
if (j == str.length - 1) {
sb.append(str[i]);
break;
}
j++;
} else {
sb.append(str[i]);
sb.append(this.partial.delimiter);
break;
}
}
i = j;
j = i + 1;
}
if ((i == str.length - 1) && (!str[i].equals(str[(i - 1)]))) {
sb.append(str[i]);
}
return sb.toString();
}
return s;
}

public static class PartialResult
{
String result;
String delimiter;
}
}
}

使用:

add jar concat_udaf.jar;
create temporary function Concat as 'com.tcc.udaf.Concat';
select a,concat(b,',') from concat_test group by a;
————————————————
转自:https://me.csdn.net/chuangchuangtao
原文链接:https://blog.csdn.net/chuangchuangtao/article/details/77455675

自定义Hive UDAF 实现相邻去重的更多相关文章

  1. Hive UDAF开发详解

    说明 这篇文章是来自Hadoop Hive UDAF Tutorial - Extending Hive with Aggregation Functions:的不严格翻译,因为翻译的文章示例写得比较 ...

  2. Hive UDAF开发之同时计算最大值与最小值

    卷首语 前一篇文章hive UDAF开发入门和运行过程详解(转)里面讲过UDAF的开发过程,其中说到如果要深入理解UDAF的执行,可以看看求平均值的UDF的源码 本人在看完源码后,也还是没能十分理解里 ...

  3. [转]hive中自定义函数(UDAF)实现多行字符串拼接为一行

    函数如何使用: hive> desc concat_test;OKa       intb       string hive> select * from concat_test;OK1 ...

  4. Hive UDAF介绍与开发

    UDAF简介 UDAF是用户自定义聚合函数.Hive支持其用户自行开发聚合函数完成业务逻辑. 通俗点说,就是你可能需要做一些特殊的甚至是非常扭曲的逻辑聚合,但是Hive自带的聚合函数不够玩,同时也还找 ...

  5. hive UDAF开发入门和运行过程详解(转)

    介绍 hive的用户自定义聚合函数(UDAF)是一个很好的功能,集成了先进的数据处理.hive有两种UDAF:简单和通用.顾名思义,简单的UDAF,写的相当简单的,但因为使用Java反射导致性能损失, ...

  6. 自定义Hive函数

    7. 函数 7.1 系统内置函数 查看系统自带的函数:show functions; 显示自带的函数的用法:desc function upper(函数名); 详细显示自带的函数的用法:desc fu ...

  7. hive UDAF开发和运行全过程

    介绍 hive的用户自定义聚合函数(UDAF)是一个很好的功能,集成了先进的数据处理.hive有两种UDAF:简单和通用.顾名思义,简单的UDAF,写的相当简单的,但因为使用Java反射导致性能损失, ...

  8. hive UDAF

    java 程序 package com.ibeifeng.udaf; import org.apache.hadoop.hive.ql.exec.UDAF; import org.apache.had ...

  9. hive UDAF源代码分析

    sss /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license a ...

随机推荐

  1. AppBoxFuture: 123挨个站-数据按序存储

      最近几天在优化存储的编码规则,顺带把之前设计了但未实现的倒排序一并实现了.由于所有数据(元数据.实体.索引等)都映射至RocksDB的Key-Value存储,所以必须扩展RocksDB的自定义比较 ...

  2. python3学习-requests使用

    前面我们讲过了urllib模块,知道他是用于网络请求的,这一节讲的requests还是用于网络请求的,只不过urllib是官方模块,而requests是第三方的模块.用过的人都说他才是'人类使用的', ...

  3. IntelliJ IDEA提升效率开发插件必备

    工欲善其事,必先利其器,好的工具可以提升我们的开发效率,下面介绍几款个人觉得比较好的编辑器插件,不仅炫酷更重要可以提高你的工作效率. 本文是作者辛苦整理的16款插件,每个都是超级实用的,不好不介绍,相 ...

  4. 100天搞定机器学习|day43 几张GIF理解K-均值聚类原理

    前文推荐 如何正确使用「K均值聚类」? KMeans算法是典型的基于距离的聚类算法,采用距离作为相似性的评价指标,即认为两个对象的距离越近,其相似度就越大.该算法认为簇是由距离靠近的对象组成的,因此把 ...

  5. cmd命令行界面运行python脚本显示的中文不正确

    在notepad++中编写了一个脚本(如图一),在cmd命令行界面中运行却发现显示的中文不正确(如图2).图3显示的是cmd界面的默认编码. 解决方案:将脚本的注释语言改为GBK,编码格式改为ANSI ...

  6. Oracle中的日期函数

    (一)查询系统的当前日期用sysdate,用法如下: select sysdate from dual 日期操作的三个格式: 日期-数字=日期 日期+=日期 日期-日期=数字(天数) (二)常用的日期 ...

  7. jquery实现表格导入到Excel(加图片)

      话不多说直接上代码 第一步:导入jquery的插件https://github.com/rainabba/jquery-table2excel HTML部分: 第二步:添加一个按钮 <but ...

  8. Codeforces 940D

    题意略. 这道题目在比赛的时候怎么想也没想明白,后来看了别人的题解才顿悟,可以说很辣鸡了. 只有b[i - 1],b[i - 2],b[i - 3],b[i - 4]相等的时候才能对答案产生限制,否则 ...

  9. JavaScript清除空格、换行,把双引号转换成单引号

    1.页面 2.源码 <!DOCTYPE> <html> <head> <meta charset="utf-8"> <titl ...

  10. React之 redux 的简单介绍及使用

    1.为什么使用redux?在小型react项目的开发中 ,view(视图层)中的数据模型(即数据),可以存放在组件中的 state 对象,换句话说页面中的动态数据存放在 state 中. 但对于开发大 ...