Esempio di linguaggio Java
Questo esempio utilizza il seguente nome di file: 'TestJavaEnvironment.java.
Codice
Le classi ambiente e libreria condivisa forniscono informazioni sull'ambiente e sulle librerie associate a un AE. Questo esempio richiede un solo argomento, un numero intero. Se l'intero è uguale a 0, l'AE emette l'ambiente che vede. Se l'intero è uguale a 1, l'AE cerca l'ambiente come libreria. Se viene trovata, viene restituito il percorso della libreria, altrimenti viene restituito NULL:
import org.netezza.ae.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class TestJavaEnvironment {
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 {
TestJavaEnvironment.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() != 1 ||
meta.getOutputNzType(0) != NzaeDataTypes.NZUDSUDX_VARIABLE) {
throw new NzaeException("expecting one output column of type
string");
}
if (meta.getInputColumnCount() != 2) {
throw new NzaeException("expecting at least two input columns");
}
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");
}
if (meta.getInputNzType(1) != NzaeDataTypes.NZUDSUDX_INT32) {
throw new NzaeException("second input column expected to be int32
type");
}
String field = (String) input.getField(0);
if (field == null)
throw new NzaeException("first input column may not be null");
Object o = input.getField(1);
if (o == null)
throw new NzaeException("second input column may not be null");
int type = (Integer)o;
if (type == 0) {
// env
NzaeEnvironment env = ae.getEnvironment();
String val = env.getValue(field);
if (val == null)
output.setField(0,null);
else
output.setField(0,val);
}
else {
NzaeLibrary libs = ae.getLibrary();
NzaeLibrary.NzaeLibraryInfo info = libs.getLibraryInfo(field,
false , NzaeLibrary.SEARCH_BOTH);
if (info == null)
output.setField(0,null);
else
output.setField(0,info.libraryFullPath);
}
}
}
public static int run(Nzae ae)
{
ae.run(new MyHandler());
return 0;
}
}Compilazione
Utilizzare la compilazione standard:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaEnvironment.java --version 3Registrazione
Registrare l'esempio:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --sig "envae(varchar(any),int)" \
--return "table(val varchar(1000))" --class AeUdtf --language java \
--template udtf --version 3 --define "java_class=TestJavaEnvironment" \
--deps inza..LIBNZAEADAPTERSQuesto esempio presuppone che sia stata aggiunta la libreria condivisa 'LIBNZAEADAPTERS.In esecuzione
Si noti che l'output di questo esempio è specifico dell'ambiente. Pertanto, il risultato effettivo sarà simile, ma non uguale, al testo seguente.
SELECT * FROM TABLE WITH FINAL(envae('inza..libnzaeadapters', 1));
VAL
------------------------------------------------------------------------
/nz/data.1.0/base/1/library/237951/host/libnzaeadapters.so
(1 row)
SELECT * FROM TABLE WITH FINAL(envae('libnzaeadapters2', 1));
VAL
-----
(1 row)
SELECT * FROM TABLE WITH FINAL(envae('NZAE_DYNAMIC_ENVIRONMENT', 0));
VAL
-----
0
(1 row)
SELECT * FROM TABLE WITH FINAL(envae('NZAE_DYNAMIC_ENVIRONMENT2', 0));
VAL
-----
(1 row)