• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

hive 复杂 UDAF 使用方法

mysql 搞代码 4年前 (2022-01-09) 22次浏览 已收录 0个评论

周海汉 /文 2013.3.27 前文《hive mapreduce script用法示例》 示例了mapreduce脚本。本文采用较复杂的方式自定义hive聚合函数。 package com.abloz.hive;/** * @author zhouhh * @date 2013-3-27 * note: for count value =1 */import org.apache.hadoop.hi

周海汉 /文
2013.3.27

前文《hive mapreduce script用法示例》
示例了mapreduce脚本。本文采用较复杂的方式自定义hive聚合函数。

package com.abloz.hive;/** * @author zhouhh * @date 2013-3-27 * note: for count value >=1 */import org.apache.hadoop.hive.ql.exec.UDAF;import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.log4j.Logger;public class JJPokerStat extends UDAF {	public static class JJCountUDAFEvaluator implements UDAFEvaluator {		public static final Logger log = Logger.getLogger(JJPokerStat.class)<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码网$</i>;		public static class PartialResult {			int total=0;			int win=0;			int fold=0;			int allin=0;		}		private PartialResult result;		public void init() {			result = new PartialResult();		}		public boolean calwin(int rbet,String chipwon)		{			if(chipwon.equalsIgnoreCase("NULL"))			{				return false;			}			String[] cw = chipwon.split("\\|");			int chipwons=0;			//log.info("calwin:"+chipwon);			for(String v:cw)			{				String[] c = v.split(":");				//log.info("calwin:v "+v+",c.length:"+c.length);				if(c.length>1)				{					chipwons += Integer.parseInt(c[1]);				}			}			//log.info("calwin:chipwons:"+chipwons+",rbet:"+rbet);			if(chipwons>rbet)			{				return true;			}			return false;		}		public boolean iterate(IntWritable rbet,Text chipwon,IntWritable f,IntWritable a) {			if ( rbet == null || chipwon == null || f == null || a == null) {				return true;			}			boolean win = calwin(rbet.get(),chipwon.toString());			if(result == null)			{				result = new PartialResult();			}			result.total++;			if(win)			{				result.win++;			}			int v = f.get();			if (v>=1)			{				result.fold++;			}			v = a.get();			if (v>=1)			{				result.allin++;			}			return true;		}		public PartialResult terminatePartial() {			return result;		}		public boolean merge(PartialResult other) {			if(other == null)			{				return true;			}			result.total+=other.total;			result.win += other.win;			result.fold += other.fold;			result.allin += other.allin;			return true;		}		public Text terminate() {			if(result == null) {				return new Text("0\t0\t0\t0");			}			String s=""+result.total+"\t"+result.win+"\t"+result.fold+"\t"+result.allin;			return new Text(s);		}	}}

相关博文:

  1. hive mapreduce script用法示例
  2. 手工移除.META.表的错误信息
  3. 编程方式将hbase数据复制到mysql


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:hive 复杂 UDAF 使用方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址