Java language example
This example uses the following file name: TestJavaEnvironment.java.
Code
The environment and shared library classes provide information about the environment and
libraries associated with an AE. This example takes one argument, an integer. If the integer equals
0, the AE outputs the environment it sees. If the integer equals 1, the AE looks the environment up
as a library. If found, the path to the library is returned, otherwise NULL is
returned:
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
Use the standard
compile:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java \
--template compile TestJavaEnvironment.java --version 3Registration
Register the
example:
$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..LIBNZAEADAPTERSThis example assumes you have added the
LIBNZAEADAPTERS shared library.Running
Note that the output from this example is specific to the environment. Therefore, your actual
output will resemble, but not match, the following
text.
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)