Función de fila simulada en lenguaje Java
Este ejemplo utiliza el siguiente nombre de archivo: ' TestJavaTime.java
Código
import org.netezza.ae.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class TestJavaTime {
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 {
TestJavaTime.run(api.aeFunction);
} finally {
api.aeFunction.close();
}
}
};
exec.execute(task);
}
}
}
helper.close();
}
public static class MyHandler implements NzaeMessageHandler
{
public void evaluate(Nzae ae, NzaeRecord input, NzaeRecord output) {
final NzaeMetadata meta = ae.getMetadata();
int op = 0;
double result = 0;
if (meta.getOutputColumnCount() != 3 ||
meta.getOutputNzType(0) != NzaeDataTypes.NZUDSUDX_INT32 ||
meta.getOutputNzType(1) != NzaeDataTypes.NZUDSUDX_INT32 ||
meta.getOutputNzType(2) != NzaeDataTypes.NZUDSUDX_INT32
) {
throw new NzaeException("expecting three output columns of "+
"type int32");
}
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");
}
int hour, min, sec;
Object field = input.getField(0);
if (field == null)
throw new NzaeException("first input column may not be null");
String sf = (String)field;
String[] ar = sf.split(":");
if (ar.length != 3)
throw new NzaeException("Badly formatted time " + sf + ". "+
"Expected h:m:s");
hour = Integer.parseInt(ar[0]);
min = Integer.parseInt(ar[1]);
sec = Integer.parseInt(ar[2]);
output.setField(0, hour);
output.setField(1,min);
output.setField(2, sec);
}
}
public static int run(Nzae ae)
{
ae.run(new MyHandler());
return 0;
}
}
Una compilación
Compila el código:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaTime.java --version 3Registro
Dado que no existe un concepto integrado de función de fila en Netezza SQL, esta función se registra como una función de tabla, aunque sólo genera una fila por cada fila de entrada:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --sig "timeae(VARCHAR(ANY))" \
--return "table(hour int4, minute int4, second int4)" --class AeUdtf \
--language java --template udtf --version 3 --define \
"java_class=TestJavaTime"En ejecución
Ejecute la consulta en nzsql:
SELECT * FROM TABLE WITH FINAL(timeae('12:30:15'));
HOUR | MINUTE | SECOND
------+--------+--------
12 | 30 | 15
(1 row)
SELECT * FROM TABLE WITH FINAL(timeae('12:30'));
ERROR: Badly formatted time 12:30. Expected h:m:s