ApplyDriver Version 2

Code in this section is saved in a file called ApplyDriverV2.java.

Code

This new version of the application driver uses environment variables to determine which function to call (OPERATION or RESULT). It also uses an environment variable to determine whether to output diagnostic information. This is a local AE only.

// ApplyDriverV2.java
package org.netezza.education;

import org.netezza.ae.*;

public class ApplyDriverV2 {
    public static final void main(String [] args) {
        NzaeFactory factory = NzaeFactory.Source.getFactory();
 
        if (factory.isLocal()) {
            NzaeInitialization init = new NzaeInitialization();
            Nzae ae = factory.getLocalFunctionApi(init);

            try {
                // determine which function is being invoked
                String whichFunction =
                    ae.getEnvironment().getValue("APPLY_FUNCTION");
                if (whichFunction == null) {
                    ae.userError(
                        "missing APPLY_FUNCTION " +
                        "environment variable");
                    return;
                }
                String printInfo =
                    ae.getEnvironment().getValue("APPLY_INFO");
                if (printInfo != null
                        && printInfo.equalsIgnoreCase("true")) {
                    ApplyUtil.printMetadata(ae);
                    ApplyUtil.printRuntime(ae);
                    ApplyUtil.printSharedLibraries(ae);
                    ApplyUtil.printEnvironment(ae);
                }

                if (whichFunction.equals("OPERATION")) {
                    ApplyOperation.runAe(ae);
                } else if (whichFunction.equals("RESULT")) {
                    ApplyResult.runAe(ae);
                } else {
                    ae.userError("unexpected value for " +
                        "environment variable APPLY_FUNCTION");
                }
            } finally {
                ae.close();
            }
        } else {
            throw new RuntimeException("Expecting Local AE only");
        }
    }
}

Compilation

$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java --template compile
--version 3 --db dev \
"ApplyDriverV2.java ApplyOperation.java ApplyResult.java ApplyUtil.java

Registration as a Scalar Function applyOperationV2Sf

This example sets the application-specific APPLY_FUNCTION AE environment variable to the function OPERATION so that the AE applies an operation to columns in a row.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udf
--version 3 --db dev \
     --sig "applyOperationV2Sf(varargs)" --return double \
     --define java_class=org.netezza.education.ApplyDriverV2 --level 1 \
     --environment "'APPLY_FUNCTION'='OPERATION'" \
     --environment "'APPLY_INFO'='true'" \
     --environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"

Registration as a Table Function applyOperationV2Tf

This example also sets the application-specific APPLY_FUNCTION AE environment variable to the function OPERATION, as above. It uses a table function invocation.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
     --sig "applyOperationV2Tf(varargs)" --return "table(result double)" \
     --define java_class=org.netezza.education.ApplyDriverV2 --level 1 \
     --environment "'APPLY_FUNCTION'='OPERATION'" \
     --environment "'APPLY_INFO'='true'" \
     --environment "'NZAE_HOST_ONLY_NZAE_SPIN_FILE_NAME'='/tmp/spin_ae.on'" \
     --environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"

Registration as Table Function applyResultV2

This example sets the application-specific APPLY_FUNCTION AE environment variable to the function RESULT so that the AE applies an operation to the second column of each row.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
     --sig "applyResultV2(varchar(16), double)" \
     --return "table(result double)" \
     --define java_class=org.netezza.education.ApplyDriverV2 --level 1 \
     --noparallel --environment "'APPLY_FUNCTION'='RESULT'" \
     --environment "'APPLY_INFO'='true'" \
     --environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"

Running as Table Function applyResultV2

SELECT result FROM edutestdata, TABLE WITH FINAL(applyResultV2('sum', f1))
     WHERE color = 'red';
 RESULT
--------
    101

Running as applyOperationV2Tf and applyResultV2

The SQL function applyOperationV2Tf applies the addition operator to the columns in a row and then streams the result to SQL function applyResultV2, which applies the sum operation to the second column in each row.

SELECT re.result FROM edutestdata,
     TABLE WITH FINAL(applyOperationV2Tf('+', f1, f2, f3, f4)) op,
     TABLE WITH FINAL(applyResultV2('sum', op.result)) re
     WHERE color = 'red';
 RESULT
--------
    413

You can create an alternate version of the ApplyDriverV2 sample, which uses third-party and open source Java code with a Java AE. This alternate version of ApplyDriverV2 uses a JAR file and is deployed to a JAR file. It calls the main method in ApplyDriverV2. Otherwise, the functionality of the AE is the same.

Acquiring the Open Source JAR File

Before you can run this example, you must download and install third-party base64 files. To retrieve and compile the sample open source JAR file, perform the following steps:
  1. Download the Java source zip file from http://iharder.sourceforge.net/current/java/base64/.
  2. Extract the contents into an empty Linux directory.
    unzip -j Base64-v<version_number>.zip
  3. Compile Base64.java:
    [~/installs/base64]$javac Base64.java
  4. To list the contents of the JAR file, enter the following command:
    [~/installs/base64]$jar tf Base64.jar
    META-INF/ 
    META-INF/MANIFEST.MF 
    Base64$1.class 
    Base64.class 
    Base64$InputStream.class 
    Base64$OutputStream.class
  5. Create the JAR file:
    [~/installs/base64]$jar cf Base64.jar *.class
  6. Deploy the newly built JAR file:
    /nz/export/ae/utilities/bin/compile_ae --language java --version 3 --db dev \ 
     --template deploy base64.jar

The JAR file is now ready to use with a Java AE.

Code for JarApplyDriverV2.java

The following AE uses the newly deployed base64.jar:

// JarApplyDriverV2.java
// demonstrates use of 3rd party jar file
package org.netezza.education;

import java.io.IOException;
// from http://iharder.sourceforge.net/current/java/base64/
import sourceforge.iharder.Base64;

public class JarApplyDriverV2 {
    public static final void main(String [] args) {
        try {
            double value = 100;
            String result = Base64.encodeObject(value);
            System.out.println("JarApplyDriverV2: "
                + result);
        } catch (IOException e) {
            System.err.println("JarApplyDriverV2: "
                + e.getMessage());
        }

        ApplyDriverV2.main(args);
    }
}

Compile arApplyDriverV2.java

This example shows how to compile using a third-party JAR file with the --linkargs parameter and how to compile to a JAR file using the –exe parameter. Because the example relies on the third-party JAR base64.jar, which was previously deployed to database dev, this AE must use the same database. (The path for the base64.jar is defined by the –linkargs option path.)

$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java --template compilejar
--version 3 --db dev \
     --linkargs /nz/export/ae/applications/dev/admin/java/base64.jar \
     --exe apply.jar \
     "JarApplyDriverV2.java ApplyDriverV2.java ApplyOperation.java \
     ApplyResult.java ApplyUtil.java"

Registration as Scalar Function applyOperationV2SfJar

This example shows how to register in order to use a class inside a JAR file using AE environment variable NZAE_APPEND_CLASSPATH. Because the example relies on the third-party JAR base64.jar, and the AE jar file apply.jar, both of which have previously been deployed to database dev, this AE must use the same database. The NZAE_APPEND_CLASSPATH environment variable allows the java CLASSPATH environment variable to be set (to locate the previously deployed base64.jar). Note the use of –exe apply.jar, which ensures that apply.jar is also added to CLASSPATH. (This assumes the compile and register steps are both in the same database, in this case dev.)

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udf
--version 3 --db dev \
     --sig "applyOperationV2SfJar(varargs)" --return double \
     --define java_class=org.netezza.education.JarApplyDriverV2 \
     --exe apply.jar --level 1 --environment "'APPLY_FUNCTION'='OPERATION'" \
     --environment "'APPLY_INFO'='true'" \
     --environment "'NZAE_LOG_DIR'='/nz/export/ae/log'" \
     --environment "'NZAE_APPEND_CLASSPATH'=\
     '/nz/export/ae/applications/dev/admin/java/base64.jar'"

Registration as Table Function applyOperationV2TfJar

This example shows how to register in order to use a class inside a JAR file using AE environment variable NZAE_APPEND_CLASSPATH, as above. It uses a table function invocation.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
     --sig "applyOperationV2TfJar(varargs)" --return "table(result double)" \
     --define java_class=org.netezza.education.JarApplyDriverV2 \
     --exe apply.jar --level 1 \
     --environment "'APPLY_FUNCTION'='OPERATION'" \
     --environment "'APPLY_INFO'='true'" \
     --environment "'NZAE_LOG_DIR'='/nz/export/ae/log'" \
     --environment "'NZAE_APPEND_CLASSPATH'=\
     '/nz/export/ae/applications/dev/admin/java/base64.jar'"

Registration as Scalar Function applyResutV2Jar

This example shows how to register in order to use a class inside a JAR file using AE environment variable NZAE_APPEND_CLASSPATH.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
     --sig "applyResultV2Jar(varchar(16), double)" 
     --return "table(result double)" \
     --define java_class=org.netezza.education.JarApplyDriverV2 \
     --exe apply.jar --level 1 --noparallel \
     --environment "'APPLY_FUNCTION'='RESULT'" \
     --environment "'APPLY_INFO'='true'" \
     --environment "'NZAE_LOG_DIR'='/nz/export/ae/log'" \
     --environment "'NZAE_APPEND_CLASSPATH'=\
     '/nz/export/ae/applications/dev/admin/java/base64.jar'"

Running JarApplyDriverV2.java

SELECT re.result FROM edutestdata,
     TABLE WITH FINAL(applyOperationV2TfJar('+', f1, f2, f3, f4)) op,
     TABLE WITH FINAL(applyResultV2Jar('sum', op.result)) re
     WHERE color = 'red';
RESULT
-------
   413