Exemple de langage Java
Cet exemple utilise le nom de fichier suivant : " TestJavaEnvironment.java.
Coder
Les classes d'environnement et de bibliothèques partagées fournissent des informations sur l'environnement et les bibliothèques associés à un AE. Cet exemple prend un argument, un entier. Si le nombre entier est égal à 0, l'AE produit l'environnement qu'il voit. Si le nombre entier est égal à 1, l'AE recherche l'environnement en tant que bibliothèque. S'il est trouvé, le chemin d'accès à la bibliothèque est renvoyé, sinon NULL est renvoyé :
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;
}
}
Compilation
Utiliser la compilation standard :
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaEnvironment.java --version 3
Enregistrement
Enregistrer l'exemple :
$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..LIBNZAEADAPTERS
Cet exemple suppose que vous avez ajouté la bibliothèque partagée " LIBNZAEADAPTERS
En cours d'exécution
Notez que les résultats de cet exemple sont spécifiques à l'environnement. Par conséquent, votre résultat ressemblera au texte suivant, mais ne lui correspondra pas.
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)