C language example
This example uses the following file name: env.c.
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:
#include <stdio.h>
#include <stdlib.h>
#include "nzaeapis.h"
static int run(NZAE_HANDLE h);
int main(int argc, char * argv[])
{
if (nzaeIsLocal())
{
NzaeInitialization arg;
memset(&arg, 0, sizeof(arg));
arg.ldkVersion = NZAE_LDK_VERSION;
if (nzaeInitialize(&arg))
{
fprintf(stderr, "initialization failed\n");
return -1;
}
run(arg.handle);
nzaeClose(arg.handle);
}
else {
NZAECONPT_HANDLE hConpt = nzaeconptCreate();
if (!hConpt)
{
fprintf(stderr, "error creating connection point\n");
fflush(stderr);
return -1;
}
const char * conPtName = nzaeRemprotGetRemoteName();
if (!conPtName)
{
fprintf(stderr, "error getting connection point name\n");
fflush(stderr);
exit(-1);
}
if (nzaeconptSetName(hConpt, conPtName))
{
fprintf(stderr, "error setting connection point name\n");
fflush(stderr);
nzaeconptClose(hConpt);
return -1;
}
NzaeremprotInitialization args;
memset(&args, 0, sizeof(args));
args.ldkVersion = NZAE_LDK_VERSION;
args.hConpt = hConpt;
if (nzaeRemprotCreateListener(&args))
{
fprintf(stderr, "unable to create listener - %s\n", \
args.errorMessage);
fflush(stderr);
nzaeconptClose(hConpt);
return -1;
}
NZAEREMPROT_HANDLE hRemprot = args.handle;
NzaeApi api;
int i;
for (i = 0; i < 5; i++)
{
if (nzaeRemprotAcceptApi(hRemprot, &api))
{
fprintf(stderr, "unable to accept API - %s\n", \
nzaeRemprotGetLastErrorText(hRemprot));
fflush(stderr);
nzaeconptClose(hConpt);
nzaeRemprotClose(hRemprot);
return -1;
}
if (api.apiType != NZAE_API_FUNCTION)
{
fprintf(stderr, "unexpected API returned\n");
fflush(stderr);
nzaeconptClose(hConpt);
nzaeRemprotClose(hRemprot);
return -1;
}
printf("testcapi: accepted a remote request\n");
fflush(stdout);
run(api.handle.function);
}
nzaeRemprotClose(hRemprot);
nzaeconptClose(hConpt);
}
return 0;
}
static int run(NZAE_HANDLE h)
{
NzaeMetadata metadata;
if (nzaeGetMetadata(h, &metadata))
{
fprintf(stderr, "get metadata failed\n");
return -1;
}
#define CHECK(value) \
{ \
NzaeRcCode rc = value; \
if (rc) \
{ \
const char * format = "%s in %s at %d"; \
fprintf(stderr, format, \
nzaeGetLastErrorText(h), __FILE__, __LINE__); \
nzaeUserError(h, format, \
nzaeGetLastErrorText(h), __FILE__, __LINE__); \
exit(-1); \
} \
}
for (;;)
{
int64_t ar[6];
int i;
double result = 0;
NzaeRcCode rc = nzaeGetNext(h);
if (rc == NZAE_RC_END)
{
break;
}
NzudsData * input = NULL;
CHECK(nzaeGetInputColumn(h, 1, &input));
int type = *input->data.pInt32;
CHECK(nzaeGetInputColumn(h, 0, &input));
if (type == 0) {
const char *result;
nzaeGetEnv(h, input->data.pVariableString, &result);
if (!result) {
CHECK(nzaeSetOutputNull(h, 0));
}
else
CHECK(nzaeSetOutputString(h, 0, result));
}
else {
const char *result = nzaeGetLibraryFullPath(h, \
input->data.pVariableString, false);
if (!result) {
CHECK(nzaeSetOutputNull(h, 0));
}
else
CHECK(nzaeSetOutputString(h, 0, result));
}
CHECK(nzaeOutputResult(h));
}
nzaeDone(h);
return 0;
}Compilation
Use the standard
compilation:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language system --version 3 \
--template compile env.c --exe envRegistration
Register the example:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language system --version 3 \
--template udtf --exe env --sig "env_c(varchar(any),int4)" \
--return "table(output varchar(1000))" --deps inza..LIBNZAEADAPTERS
This 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(env_c('inza..libnzaeadapters', 1));
OUTPUT
---------------------------------------------------------------------
/nz/data.1.0/base/1/library/237951/host/libnzaeadapters.so
(1 row)
SELECT * FROM TABLE WITH FINAL(env_c('NZAE_DYNAMIC_ENVIRONMENT', 0));
OUTPUT
--------
0
(1 row)
SELECT * FROM TABLE WITH FINAL(env_c('NZAE_DYNAMIC_ENVIRONMENT2', 0));
OUTPUT
--------
(1 row)