Agrégats en langage Java

Cet exemple utilise le nom de fichier suivant : " TestJavaMax.java.

Coder

Le code de cet exemple est légèrement plus long que celui de l'exemple de l'agrégat en langage C, car il est conçu pour traiter tous les types de données possibles.
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());
}
}

Compilation

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

Enregistrement

Enregistrez l'exemple en tant qu'UDA en utilisant le paramètre " 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"

En cours d'exécution

Pour l'exécuter, créez d'abord une table fictive, puis exécutez l'agrégat :

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)