hive
사용자 정의 집계 함수 (UDAF)
수색…
UDAF 평균 예
org.apache.hadoop.hive.ql.exec.hive.UDAF
를 확장 한 Java 클래스 만들기UDAFEvaluator
를 구현하는 내부 클래스 만들기5 가지 방법 구현
-
init()
-이 메소드는 평가자를 초기화하고 내부 상태를 재설정합니다. 아래의 코드에서 새로운 Column ()을 사용하여 아직 값이 집계되지 않았 음을 나타냅니다. -
iterate()
-이 메서드는 집계 할 새 값이있을 때마다 호출됩니다. 평가자는 집계 수행 결과로 내부 상태를 갱신해야한다 (우리는 총합을 수행 중이다. 아래 참조). 입력이 유효 함을 나타 내기 위해 true를 반환합니다. -
terminatePartial()
-이 메소드는 Hive가 부분 집계에 대한 결과를 원할 때 호출됩니다. 메서드는 집계 상태를 캡슐화하는 객체를 반환해야합니다. -
merge()
- Hive가 하나의 부분 집계를 다른 집계와 결합하도록 결정할 때이 메서드가 호출됩니다. -
terminate()
-이 메서드는 집계의 최종 결과가 필요할 때 호출됩니다.
-
public class MeanUDAF extends UDAF {
// Define Logging
static final Log LOG = LogFactory.getLog(MeanUDAF.class.getName());
public static class MeanUDAFEvaluator implements UDAFEvaluator {
/**
* Use Column class to serialize intermediate computation
* This is our groupByColumn
*/
public static class Column {
double sum = 0;
int count = 0;
}
private Column col = null;
public MeanUDAFEvaluator() {
super();
init();
}
// A - Initalize evaluator - indicating that no values have been
// aggregated yet.
public void init() {
LOG.debug("Initialize evaluator");
col = new Column();
}
// B- Iterate every time there is a new value to be aggregated
public boolean iterate(double value) throws HiveException {
LOG.debug("Iterating over each value for aggregation");
if (col == null)
throw new HiveException("Item is not initialized");
col.sum = col.sum + value;
col.count = col.count + 1;
return true;
}
// C - Called when Hive wants partially aggregated results.
public Column terminatePartial() {
LOG.debug("Return partially aggregated results");
return col;
}
// D - Called when Hive decides to combine one partial aggregation with another
public boolean merge(Column other) {
LOG.debug("merging by combining partial aggregation");
if(other == null) {
return true;
}
col.sum += other.sum;
col.count += other.count;
return true;
}
// E - Called when the final result of the aggregation needed.
public double terminate(){
LOG.debug("At the end of last record of the group - returning final result");
return col.sum/col.count;
}
}
}
hive> CREATE TEMPORARY FUNCTION <FUNCTION NAME> AS 'JAR PATH.jar';
hive> select id, mean_udf(amount) from table group by id;
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow