Aggregati in linguaggio Java

Questo esempio utilizza il seguente nome di file: 'TestJavaMax.java.

Codice

Il codice di questo esempio è leggermente più lungo di quello dell'aggregato in linguaggio C, poiché è stato progettato per gestire tutti i tipi di dati possibili.
import java.io.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.netezza.ae.*;
public class TestJavaMax {
private static final Executor exec =
Executors.newCachedThreadPool();
public static final void main(String [] args) {
try {
mainImpl(args);
} catch (Throwable t) {
System.err.println(t.toString());
NzaeUtil.logException(t, "main");
}
}
public static final void mainImpl(String [] args) {
NzaeApiGenerator helper = new NzaeApiGenerator();
while (true) {
final NzaeApi api = helper.getApi(NzaeApi.AGGREGATION);
if (api.apiType == NzaeApi.AGGREGATION) {
if (!helper.isRemote()) {
run(api.aeAggregate);
break;
} else {
Runnable task = new Runnable() {
public void run() {
try {
TestJavaMax.run(api.aeAggregate);
} finally {
api.aeAggregate.close();
}
}
};
exec.execute(task);
}
}
}
helper.close();
}

public static class MyHandler implements NzaeAggMessageHandler
{
public void initializeState(NzaeAgg api,
NzaeRecord state) {
state.setField(0, null);
}
public void accumulate(NzaeAgg api,
NzaeRecord input,
NzaeRecord state) {
Object field = input.getField(0);
if (field == null)
return;
Object statefield = state.getField(0);
if (statefield == null) {
statefield = field;
}
else {
if (((Comparable)statefield).compareTo(field) < 0)
statefield = field;
}
state.setField(0, statefield);
}
public void merge(NzaeAgg api,
NzaeRecord inputState,
NzaeRecord state) {
Object field = inputState.getField(0);
if (field == null)
return;
Object statefield = state.getField(0);
if (statefield == null) {
statefield = field;
}
else {
if (((Comparable)statefield).compareTo(field) < 0)
statefield = field;
}
state.setField(0, statefield);
}
public void finalResult(NzaeAgg api,
NzaeRecord inputState,
NzaeRecord result) {
Object field = inputState.getField(0);
result.setField(0, field);
}
}
private static final void run(NzaeAgg ae) {
ae.runAggregation(new MyHandler());
}
}

Compilazione

Utilizzare la compilazione standard:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaMax.java –-version 3

Registrazione

Registrare l'esempio come UDA utilizzando il parametro 'state:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --sig "maxae(int)" \
--return "int4" --state "(int4)" --class AeUda --language java \
--template uda --version 3 --define "java_class=TestJavaMax"

In esecuzione

Per eseguirlo, creare prima una tabella fittizia e poi eseguire l'aggregato:

CREATE TABLE grp_test (grp int4, val int4);
CREATE TABLE
INSERT INTO grp_test VALUES (1, 1);
INSERT 0 1
INSERT INTO grp_test VALUES (1, 2);
INSERT 0 1
INSERT INTO grp_test VALUES (1, 3);
INSERT 0 1
INSERT INTO grp_test VALUES (2, 4);
INSERT 0 1
SELECT maxae(val) FROM grp_test;
MAXAE
-------
4
(1 row)

SELECT grp, maxae(val) FROM grp_test GROUP BY grp;
GRP | MAXAE
-----+-------
1 | 3
2 | 4
(2 rows)
SELECT grp, maxae(val) over (partition BY grp) FROM grp_test;
GRP | MAXAE
-----+-------
1 | 3
1 | 3
1 | 3
2 | 4
(4 rows)