Running R AEs in details

Learn how to run R As (Analytic Executables).

About this task

R Analytic Executables (AEs) are built on top of regular AEs to support the R programming language with the R environment on Netezza. The layer that handles R on Netezza is called the R Language Adapter, or the R Adapter.

The following R code sample, which assumes at least one input column, calculates the square root of the first input column and outputs the result as the only output column.
nz.fun <- function() {
while(getNext()) {
x <- getInputColumn(0)
setOutputDouble(0,sqrt(as.double(x)))
outputResult()
}
}

Procedure

  1. Create a /tmp/rae.R file and enter the code. After you save the file, you can compile it. The following sample command starts the compilation tool, assuming that the NZ_EXPORT_DIR environment variable points to /nz/export:
    /nz/export/ae/utilities/bin/compile_ae --language r \
    --template compile --version 3 --db dev --user nz /tmp/rae.R

    This command creates the rae.R file that is located in /nz/export/ae/applications/dev/nz/. The last two directories match the database name and the username that is specified in the command.

  2. The last step to run the R Analytic Executable from SQL is registration. The sample code for registering the compiled file in the dev database:
    /nz/export/ae/utilities/bin/register_ae --language r \
    --template udf --version 3 --db dev --user nz \
    --sig 'rae(double)' --return 'double' --exe 'rae.R'
    The following switches that appear in the command are:
    --sig
    Defines the UDX name along with the input signature.
    --return
    Defines the output signature or result type.
    --exe
    Points to the specific file that was compiled.
    You must save this file under /nz/export/ae/applications/dev/nz/. The full path is determined from the values of the --db and --user switches.
  3. After this command is completed, you can log in to the dev database and run the new UDX:
    SELECT rae(column_name) FROM table_name;

    When you run the UDX, every data partition starts a new R process and starts the R Adapter that calls the nz.fun function from the file.

    A sample output:
    RAE
    -----------------
    2.5099800796022
    2.4083189157585
    (2 rows)