最近学习hive写udaf,想实现自定义count,判断如果字段为1就计数,代码如下:
-
- <P> package com.job;</P>
- <P>import org.apache.hadoop.hive.ql.exec.UDAF;
- import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
- import org.apache.hadoop.io.IntWritable;</P>
- <P>
- public class MaxiNumber extends UDAF{</P>
- <P> public static class MaxiNumberIntUDAFEvaluator implements UDAFEvaluator{
-
- private IntWritable result;
- @Override
- public void init() {
- result=null;
- }
-
- public boolean iterate(IntWritable value){
- if(null == value){
- return false;
- }
- else if(null == result){
- result=new IntWritable(0);
- return true;
- }
- else{</P>
- <P> if(value.get()==1){
- result.set(result.get()+1);</P>
- <P> }
- return true;
- }
- }
-
- public IntWritable terminatePartial(){
- return result;
- }
-
- public boolean merge(IntWritable other){
- return iterate(other);
- }
-
- public IntWritable terminate(){
- return result;
- }
- }</P>
- <P>}
- </P>
复制代码
为什么跑出来结果都是1呢?
|