Transformation Functions

Some standard transformations support transformation functions. Transformation functions are classes that will derive data based on an input record provided. All functions must implement interface Function (see Function).

The following transformation functions are provided. We expect additional functions over time.

Function Description
Count Will count the number of records (see Count)
Average Will average a numeric field over a number of records (see Average)
Minimum Will find the minimum value over a number of records (see Minimum)
Maximum Will find the maximum value over a number of records (see Maximum)

For a usage example, see Aggregator usage example.

Next section.

Writing Functions

All functions must implement interface Function (see Function). Function is generic, so it's best if you supply a data type. As an example, I'll use the Count function provided with Transform4J.

Create a class that implements the Function interface. You'll be required to implement several methods. Let's start with compute(), which is called for every record for which the function is required to derive a value. In the case of this count, all we need to do is increment a counter that starts at 0 when the function is instantiated.

public class Count implements Function<DataRecord> {

	private long currentCount = 0;

	@Override
	public void compute(DataRecord inputRecord) {
		if (inputRecord != null) {
			currentCount++;
		}	
	}
	
}
			 	

As the caller will need the computed value at some point, you're required to implement the getValue() method.

public class Count implements Function<DataRecord> {

	private long currentCount = 0;

	@Override
	public Object getValue() {
		return Long.valueOf(currentCount);
	}
	
}
			 	

Depending on context, the caller may need copy instances of this counter (already configured) for use on other records. Consequently, you are required to implement copy().

public class Count implements Function<DataRecord> {

	private long currentCount = 0;

	@Override
	public Function<DataRecord> copy() {
		Count c = new Count();
		c.currentCount = this.currentCount;
		return c;
	}
	
}
			 	

Next section.