R language table function (remote mode) 2
This example shows how you can use the remote mode to save the data processing state between subsequent queries.
Code
Define the usual set of R AE objects by saving them in the /tmp/remote.R file as follows:
nz.fun <- function () {
if (!exists('x', envir=.GlobalEnv))
x <<- 0
x <<- x + 1
getNext()
setOutput(0, x)
outputResult()
}
Here, the global assignment operator <<- ensures that the same counter x is available in all calls.
Compilation
Here, the global assignment operator <<- ensures that the same counter x is available in all calls.
/nz/export/ae/utilities/bin/compile_ae --language r \
--template compile --version 3 --db dev --user nz /tmp/remote.R
Registration
Register the launcher as follows:
/nz/export/ae/utilities/bin/register_ae --language r \
--version 3 --template udtf --db dev --user nz \
--level 4 --mask DEBUG --sig "remote_rae_launch(int8)" \
--return "TABLE(aeresult varchar(255))" \
--remote --rname remote_rae --launch
Next, register the actual data-processing interface:
/nz/export/ae/utilities/bin/register_ae --language r \
--version 3 --template udtf --db dev --user nz --level 4 \
--mask DEBUG --lastcall 'table final' --exe remote.R \
--sig "remote_rae(VARARGS)" --return "TABLE(cnt DOUBLE)" \
--remote --rname remote_rae
Running
Running a remote AE requires two steps.
In the first step, launch the remote AE as follows:
SELECT * FROM TABLE WITH FINAL(REMOTE_RAE_LAUNCH(0));
Sample output might look like the following:
AERESULT
----------------------------------------------------
tran: 91488 session: 16067 data slc: 0 hardware: 0 \
machine: netezza process: 13230 thread: 13230
(1 row)
Now run the actual data processing query:
SELECT * FROM TABLE WITH FINAL(remote_rae(0));
CNT
-----
1
(1 row)
Subsequent calls should return 2, 3, and so on, as the x global object is incremented each time the remote AE is invoked.
R Language Shapes & Sizer with Remote AEs
If the /tmp/remote.R file is updated to include the last two lines of the code sample below, the remote R AE can be registered and run with TABLE(ANY) specified as its output.
nz.fun <- function () {
if (!exists('x', envir=.GlobalEnv))
x <<- 0
x <<- x + 1
getNext()
setOutput(0, x)
outputResult()
}
nz.shaper <- 'std'
nz.shaper.list <- list(value=NZ.DOUBLE)
Note that the new variables, nz.shaper and nz.shaper.list are parsed only if the UDAP is registered with TABLE(ANY). As a result, there is no need to change the compiled file name as the file is overwritten.
Compilation is therefore the same as in the Remote Table Function case. Registration of the launcher UDX is also the same. However, the data-processing interface registration has TABLE(ANY) as its output signature and a different UDX name:
/nz/export/ae/utilities/bin/register_ae --language r \
--version 3 --template udtf --db dev --user nz --level 4 \
--mask DEBUG --lastcall 'table final' --exe remote.R \
--sig "remote_rae2(VARARGS)" --return "TABLE(ANY)" \
--remote --rname remote_rae
SELECT * FROM TABLE WITH FINAL(remote_rae(0));
CNT
-----
4
(1 row)
SELECT * FROM TABLE WITH FINAL(remote_rae2(0));
VALUE
-------
5
(1 row)