ApplyDriver Version 4
Code in this section is saved in a file called ApplyDriverV4.java.
Concepts
ApplyDriverV4 replaces ApplyDriverV3 and includes logic for obtaining both remote and local data connections. This uses the lower level (less user friendly) methods instead of the easier to use but less functional NzaeApiGenerator class to support both local and remote modes.
This technique is most useful with remote AEs not launched with the AE system. It also illustrates how NzaeApiGenerator works.
When a remote AE is launched by the AE runtime system, the connection point settings typically come from the AE environment indirectly via methods such as getRemoteName. However, the AE connection point settings can be literals or come from any source.
Code
// ApplyDriverV4.java
// demonstrates Remote AE
package org.netezza.education;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.netezza.ae.*;
public class ApplyDriverV4 {
private static final Executor exec =
Executors.newCachedThreadPool();
public static final void main(String [] args) {
NzaeFactory factory = NzaeFactory.Source.getFactory();
if (factory.isLocal()) {
DataConnection.useApi(factory.getLocalApi());
} else {
NzaeConnectionPoint conpt =
factory.newConnectionPoint();
conpt.setName(conpt.getRemoteName());
conpt.setSessionId(conpt.getRemoteSessionId());
conpt.setTransactionId(
conpt.getRemoteTransactionId());
conpt.setDataSliceId(conpt.getRemoteDataSliceId());
System.out.println("listening on: "
+ " name=" + conpt.getName()
+ ", data slice=" + conpt.getDataSliceId()
+ ", transaction=" + conpt.getTransactionId()
+ ", session=" + conpt.getSessionId());
NzaeRemoteProtocol remprot =
factory.createListener(conpt);
for (;;)
{
final NzaeApi api = remprot.acceptConnection();
Runnable task = new Runnable() {
public void run() {
DataConnection.useApi(api);
}
}
exec.execute(task);
}
}
}
}
Compilation
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java --template compile
--version 3 --db dev \
"ApplyDriverV4.java DataConnection.java ApplyOperation.java \
ApplyResult.java ApplyUtil.java CloneRows.java"
Registration
These registrations are for the same SQL functions used previously, named with different version numbers.
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udf
--version 3 --db dev \
--sig "applyOperationRemV4Sf(varargs)" --return double \
--define java_class="" --remote --rname apply --level 1 \
--environment "'APPLY_FUNCTION'='OPERATION'" \
--environment "'APPLY_INFO'='true'" \
--environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
--sig "applyOperationRemV4Tf(varargs)" --return "table(result double)" \
--define java_class="" --remote --rname apply --level 1 \
--environment "'APPLY_FUNCTION'='OPERATION'" \
--environment "'APPLY_INFO'='true'" \
--environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
--sig "applyResultRemV4(varchar(16), double)" \
--return "table(result double)" --define java_class="" \
--remote --rname apply --level 1 --noparallel \
--environment "'APPLY_FUNCTION'='RESULT'" \
--environment "'APPLY_INFO'='true'" \
--environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
--sig "cloneRowsRemV4(varargs)" --return "table(any)" \
--define java_class="" --remote --rname apply --dynamic 2 --level 1 \
--environment "'APPLY_FUNCTION'='CLONE'" \
--environment "'APPLY_INFO'='true'" \
--environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"
Registration for Launching
This registration is for an SQL function to launch this remote AE.
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
--sig "apply_launch_v1(bigint)" --return "TABLE(aeresult varchar(255))" \
--define java_class=org.netezza.education.ApplyDriverV4 --launch \
--rname apply --level 1 --environment "'APPLY_FUNCTION'='CLONE'" \
--environment "'APPLY_INFO'='true'" \
--environment "'NZAE_LOG_DIR'='/nz/export/ae/log'"
Launching on the Host
SELECT aeresult FROM TABLE WITH FINAL(apply_launch_v1(0));
AERESULT
---------------------------------------------------
tran: 17928 session: 16522 DATA slc: 0 hardware: 0
machine: hostlnx1 process: 27184 thread: 27185
Pinging on the Host
SELECT aeresult FROM TABLE WITH FINAL(inza..nzaejobcontrol('ping', 0, 'apply',
false, NULL, NULL));
AERESULT
--------------------------------------------------------
hostlnx1 27184 (apply dataslc:-1 sess:-1 trans:-1)
thread: 27185 AE Build: 10 nzrep version: 9
Launching on the SPUs
SELECT aeresult FROM _v_dual_dslice, TABLE WITH FINAL(apply_launch_v1(dsid));
AERESULT
------------------------------------------------------
tran: 17916 session: 16522 DATA slc: 4 hardware: 1002
machine: spu0101 process: 21751 thread: 21755
Pinging on the SPUs
SELECT aeresult FROM _v_dual_dslice, TABLE WITH FINAL(inza..nzaejobcontrol(
'ping', dsid, 'apply', false, NULL, NULL));
AERESULT
----------------------------------------------------
spu0101 21751 (apply dataslc:-1 sess:-1 trans:-1)
thread: 21755 AE Build: 10 nzrep version: 9
Running
SELECT re.result FROM edutestdata, TABLE WITH FINAL(cloneRowsRemV4(f1, f2,
f3, f4, 'CLONE_COUNT=3, CLONE_COLUMN_NAMES="col1,col2,col3,col4"')),
TABLE WITH FINAL(applyOperationRemV4Tf('+', col1, col2, col3, col4)) op,
TABLE WITH FINAL(applyResultRemV4('sum', op.result)) re
WHERE color = 'red';
RESULT
--------
1239
Stopping on the Host
SELECT aeresult FROM TABLE WITH FINAL(inza..nzaejobcontrol('stop', 0, 'apply',
false, NULL, NULL));
AERESULT
--------------------------------------------------------------
hostlnx1 27184 (apply dataslc:-1 sess:-1 trans:-1) AE stopped
Stop ApplyDriverV4.java on the S-Blades
SELECT aeresult FROM _v_dual_dslice, TABLE WITH FINAL(inza..nzaejobcontrol('stop',
dsid, 'apply', false, NULL, NULL));
AERESULT
--------------------------------------------------------------
spu0101 21751 (apply dataslc:-1 sess:-1 trans:-1) AE stopped
SELECT aeresult FROM _v_dual_dslice, TABLE WITH
final(inza..nzaejobcontrol('cleanup', dsid, 'apply', false, NULL, NULL));
AERESULT
----------
(0 rows)