Created
March 22, 2012 14:49
-
-
Save tf0054/2158749 to your computer and use it in GitHub Desktop.
Hack#38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.hadoophacks.hive; | |
| import org.apache.hadoop.hive.serde2.io.DoubleWritable; | |
| import org.apache.hadoop.io.IntWritable;; | |
| import org.apache.hadoop.hive.ql.exec.*; | |
| public class Average extends UDAF{ | |
| /** | |
| * | |
| *途中計算結果を格納しておくクラス | |
| * | |
| */ | |
| public static class TempResult{ | |
| private DoubleWritable sum = new DoubleWritable(0); | |
| private IntWritable num = new IntWritable(0); | |
| public DoubleWritable getSum(){ | |
| return this.sum; | |
| } | |
| public IntWritable getNum(){ | |
| return this.num; | |
| } | |
| public void addSum(double value){ | |
| sum.set(sum.get() + value); | |
| } | |
| public void addNum(int value){ | |
| num.set(num.get() + value); | |
| } | |
| } | |
| public static class AverageEvaluator implements UDAFEvaluator{ | |
| private TempResult tempResult; | |
| public void init(){ | |
| //有効な値が1件も無かった場合のために、nullで初期化しておく。 | |
| tempResult = null; | |
| } | |
| public boolean iterate(DoubleWritable value){ | |
| if(tempResult == null){ | |
| tempResult = new TempResult(); | |
| } | |
| tempResult.addSum(value.get()); | |
| tempResult.addNum(1); | |
| return true; | |
| } | |
| public TempResult terminatePartial(){ | |
| return tempResult; | |
| } | |
| public boolean merge(TempResult otherTemp){ | |
| if(tempResult == null){ | |
| tempResult = otherTemp; | |
| } | |
| else if(otherTemp != null){ | |
| double sum = otherTemp.getSum().get(); | |
| int num = otherTemp.getNum().get(); | |
| tempResult.addSum(sum); | |
| tempResult.addNum(num); | |
| } | |
| return true; | |
| } | |
| public DoubleWritable terminate(){ | |
| double sum = tempResult.getSum().get(); | |
| int num = tempResult.getNum().get(); | |
| return new DoubleWritable(sum/num); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment