Java Sprache einfache Tabellenfunktion
In diesem Beispiel wird der folgende Dateiname verwendet: ' TestJavaSplit.java
Code erstellen
Der Code in diesem Beispiel erstellt eine Funktion, die eine Zeichenkette auf der Grundlage des Trennzeichens in mehrere Zeichenketten aufteilt. Dieses Beispiel kann mehr Zeilen mit Ausgaben als mit Eingaben liefern. Aus diesem Grund kann es das Handler-Modell nicht verwenden und erfordert eine zusätzliche Fehlerbehandlungslogik. Beachten Sie den try-catch-Block und die Schleifenlogik in run. Beachten Sie auch, dass Code bereitgestellt werden muss, um die Datensatzobjekte abzurufen, das Objekt auszugeben und die Datensatzobjekte zu löschen.
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;
}
}
Kompilierung
Verwenden Sie die Standardkompilierung
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaSplit.java --version 3Registrierung
Registrieren Sie die neu erstellte Datei:
$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"Aktiv
Führen Sie die Abfrage in nzsql aus:
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