Fonction de tableau simple en langage Java

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

Coder

Le code de cet exemple crée une fonction qui divise une chaîne en plusieurs chaînes en fonction du délimiteur. Cet exemple peut renvoyer plus de lignes de sortie que de lignes d'entrée. Pour cette raison, il ne peut pas utiliser le modèle de gestionnaire et nécessite une logique supplémentaire de gestion des erreurs. Notez le bloc try catch et la logique de la boucle dans run. Notez également que du code doit être fourni pour récupérer les objets d'enregistrement, éditer l'objet et supprimer les objets d'enregistrement.
import org.netezza.ae.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class TestJavaSplit {
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.FUNCTION);
if (api.apiType == NzaeApi.FUNCTION) {
if (!helper.isRemote()) {
run(api.aeFunction);
break;
} else {
Runnable task = new Runnable() {
public void run() {
try {
TestJavaSplit.run(api.aeFunction);
} finally {
api.aeFunction.close();
}
}
};
exec.execute(task);
}
}
}
helper.close();
}
public static int run(Nzae ae) {
try {
int ret =runReal(ae);
ae.done();
return ret;
}
catch (NzaeException ex) {
ae.userError(ex.getMessage());
ae.done();
throw ex;
}
catch (Throwable ex) {
ae.userError(ex.getMessage());
ae.done();
throw new NzaeException(ex.getMessage());
}
}
public static int runReal(Nzae ae) {
final NzaeMetadata meta = ae.getMetadata();
if (meta.getOutputColumnCount() != 1 ||
meta.getOutputNzType(0) != NzaeDataTypes.NZUDSUDX_VARIABLE
) {
throw new NzaeException("expecting one output columns of type "+
"string");
}
if (meta.getInputColumnCount() != 1) {
throw new NzaeException("expecting one input column");
}
if (meta.getInputNzType(0) != NzaeDataTypes.NZUDSUDX_FIXED &&
meta.getInputNzType(0) != NzaeDataTypes.NZUDSUDX_VARIABLE) {
throw new NzaeException("first input column expected to be a "+
"string type");
}
for (;;) {
NzaeRecord input = ae.next();
if (input == null)
break;
NzaeRecord output = ae.createOutputRecord();
String field = (String) input.getField(0);
if (field == null)
throw new NzaeException("first input column may not be null");
String[] words = field.split(",");
for (int i=0; i < words.length; i++) {
output.setField(0, words[i]);
ae.outputResult(output);
}
input = null;
output = null;
}
return 0;
}
}

Compilation

Utiliser la compilation standard
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaSplit.java --version 3

Enregistrement

Enregistrer le fichier nouvellement créé :
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --sig "splitae(VARCHAR(ANY))" \
--return "table(val varchar(2000))" --class AeUdtf --language java \
--template udtf --version 3 --define "java_class=TestJavaSplit"

En cours d'exécution

Exécutez la requête dans nzsql :
SELECT * FROM TABLE WITH FINAL(splitae('12:30:15'));
VAL
----------
12:30:15
(1 row)
SELECT * FROM TABLE WITH FINAL(splitae('12:30:15,two,three'));
VAL
----------
12:30:15
two
three
(3 rows)
SELECT * FROM TABLE WITH FINAL(splitae(NULL));
ERROR: first input COLUMN may NOT be NULL