Recherche…


UDAF signifie exemple

  • Créez une classe Java qui étend org.apache.hadoop.hive.ql.exec.hive.UDAF Créez une classe interne qui implémente UDAFEvaluator

  • Mettre en œuvre cinq méthodes

    • init() - Cette méthode initialise l'évaluateur et réinitialise son état interne. Nous utilisons new Column () dans le code ci-dessous pour indiquer qu'aucune valeur n'a encore été agrégée.
    • iterate() - Cette méthode est appelée chaque fois qu'il y a une nouvelle valeur à agréger. L'évaluateur doit mettre à jour son état interne avec le résultat de l'agrégation (nous faisons la somme - voir ci-dessous). Nous retournons true pour indiquer que l'entrée était valide.
    • terminatePartial() - Cette méthode est appelée lorsque Hive souhaite un résultat pour l'agrégation partielle. La méthode doit renvoyer un objet qui encapsule l'état de l'agrégation.
    • merge() - Cette méthode est appelée lorsque Hive décide de combiner une agrégation partielle avec une autre.
    • terminate() - Cette méthode est appelée lorsque le résultat final de l'agrégation est requis.
    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
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow