UDF Buffer Assembly


Number of input and output link

Number of input link : One
Number of output link : One

UDF Buffer Assembly () grouped and sorted data using a Java-based buffer function defined by the user.
It processes a group of records at a time and may emit zero or more output records.


Unlike record-based transformations, this assembly processes one group of records at a time and may emit zero or more output records for each group.

This assembly must be preceded by a Group By (or Group By–based) assembly.

Parameters of the UDF Buffer

Creating a User Defined Buffer

A User Defined Buffer is created by implementing a Java class that:

Methods Provided by BufferFunction247

The class com.labs247.buffer.BufferFunction247 provides the following methods:
  1. public abstract void operate(Iterator<List<Comparable>> records)
    Called once for each group.
    Receives an iterator over all records in the group.
  2. public String getParameter()
    Returns the parameter value passed to the buffer.
  3. public void prepare()
    Called immediately after the buffer object is created. Used for initialization logic.
  4. public void cleanup()
    Called after all groups have been processed.
    Used for resource cleanup.
  5. public final void addToOutputCollector(List<Comparable> record)
    Emits a complete output record (multiple fields).
  6. public final void addToOutputCollector(Comparable value)
    Emits a single-field output record.
  7. public List<String> getIncomingFields()
    Returns the list of field names from the incoming records.
  8. public long getRecordNumber()
    Returns the number of records in the current group.

Example: Java-Based User Defined Buffer

The following example calculates minimum, maximum, and average values for a grouped dataset.
package hgrid.plugins.buffer;

import java.util.Iterator;
import java.util.List;

public class MinMaxAvg extends com.labs247.buffer.BufferFunction247 {
    Comparable min;
    Comparable max;
    double sum=0.0;
    
    @Override
    public void operate(Iterator<List<Comparable>> records) {
        while (records.hasNext()){
            List<Comparable> record=records.next();
            Comparable data=record.get(1);
            if (min==null){
                min=data;
            }else{
                min= min.compareTo(data)<0? min:data;
            }
            if (max==null){
                max=data;
            }else{
                max= max.compareTo(data)>0? max:data;
            }
            try{
                sum=sum+Double.parseDouble(data.toString());
            }catch(Exception e){}
        }
        sum=sum/getRecordNumber();
        String result=min.toString()+this.getParameter()+
                      max.toString()+this.getParameter()+
                      sum;
        addToOutputCollector(result);
    }  
}

Deploying the UDF Buffer

To make the UDF Buffer available in the HGrid247-3.5 GUI:

Required Dependency

Java-based plug-ins require the following library:
    libCommon/HGrid247CommonLib-3.5.jar

Summary

The UDF Buffer Assembly:

Supported Engines

Map
Reduce
Spark 1.x
RDD
Spark 2.x
RDD
Spark 2.x
Dataset
Apache Beam
2.29.0 Batch
Apache Beam
2.29.0 Streaming
Apache Flink
1.12.0 Batch
Apache Flink
1.12.0 Streaming
Apache Tez
0.9.1
PySpark