Skip to Content

Connect to Data Lake IQ Using the Node.js Driver

test
0 %
Connect to Data Lake IQ Using the Node.js Driver
Details

Connect to Data Lake IQ Using the Node.js Driver

July 8, 2021
Created by
May 13, 2021
Create and debug a Node.js application that connects to data lake IQ.

You will learn

  • How to create and debug a Node.js application that connects to and queries a data lake IQ database
QR code

Prerequisites

  • You have completed the first two tutorials in this group.

Node.js provides a JavaScript runtime outside of the browser and uses an asynchronous event driven programming model. For more details, see Introduction to Node.js.


Step 1: Install Node.js
  1. Ensure you have Node.js installed and check its version. Enter the following command:
    node -v  
    

    If Node.js is installed, the currently installed version is returned, such as v12.14.1.

    Note that the supported versions of Node.js are listed at node sqlanywhere.

    If Node.js is not installed, on Microsoft Windows, download the long-term support (LTS) version 12 of Node.js from Download Node.js.

    The following command will install Node 12 on openSUSE Leap 15.2.

    sudo zypper install nodejs12
    sudo zypper install npm
    
Log on to answer question
Step 2: Install additional dependencies for native compilation on Linux

The driver requires compilation on Linux. On Microsoft Windows, these libraries are included when the driver is downloaded from NPM.

Compilation is managed by node-gyp.

  1. Install node-gyp with the following command:

    sudo npm install -g node-gyp
    
  2. node-gyp also has additional requirements.

    Dependencies
    Python v3.6, v3.7, v3.8, or v3.9
    A C/C++ compiler like GCC
    make

    The following commands can be used to check if the software is installed and the version:

    python3 --version
    gcc -v
    g++ --version
    make -v
    

    The following commands will install these on openSUSE Leap 15.2.

    sudo zypper install nodejs12-devel
    sudo zypper install python3
    sudo zypper install gcc
    sudo zypper install gcc-c++
    sudo zypper install make
    
Log on to answer question
Step 3: Install Node.js driver from NPM

Node.js packages are available using NPM, which is the standard package manager for Node.js.

  1. Enter sqlanywhere and click Search.

    Search for sqlanywhere

    The page for the SQL Anywhere Node.js package on NPM is shown below. Note that this same driver is used for data lake IQ and SQL Anywhere.

  2. Create a folder named node and enter the newly created directory.

    mkdir %HOMEPATH%\DataLakeClientsTutorial\node
    cd %HOMEPATH%\DataLakeClientsTutorial\node
    
    mkdir -p ~/DataLakeClientsTutorial/node
    
  3. Initialize the project and install the driver from NPM.

    npm init -y
    npm install sqlanywhere
    

    The driver is also available at GitHub and can alternatively be used as shown below.

    Download a zip of the project.

    download from GitHub

    Extract the driver to the data lake client SDK folder.

    Extract driver

    Initialize the project and build and install the driver.

    npm init -y
    npm install %IQDIR17%\SDK\node-sqlanywhere-master
    
  4. The following command lists the Node.js modules that are now installed locally into the DataLakeClientsTutorial\node folder.

    npm list --depth=2
    
    npm list
Log on to answer question
Step 4: Create a Node.js application that queries data lake IQ
  1. Open a file named nodeQuery.js in an editor.

    notepad nodeQuery.js
    
    nano nodeQuery.js
    
  2. Add the code below to nodeQuery.js.

    'use strict';
    const { PerformanceObserver, performance } = require('perf_hooks');
    var t0;
    var util = require('util');
    var datalakeIQ = require('sqlanywhere');
    
    var connOptions = {
        //Specify the connection parameters
        host: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX.iq.hdl.trial-XXXX.hanacloud.ondemand.com:443',
        uid: 'USER1',
        pwd: 'Password1',
        enc: 'TLS{tls_type=rsa;direct=yes}',
    };
    
    //Synchronous  example querying a table
    var connection = datalakeIQ.createConnection();
    connection.connect(connOptions);
    
    var sql = 'select TITLE, FIRSTNAME, NAME from CUSTOMER;';
    t0 = performance.now();
    var result = connection.exec(sql);
    console.log(util.inspect(result, { colors: false }));
    var t1 = performance.now();
    console.log("time in ms " +  (t1 - t0));
    connection.disconnect();
    
    //Asynchronous example calling a stored procedure
    var connection = datalakeIQ.createConnection();
    connection.connect(connOptions, function(err) {
        if (err) {
            return console.error(err);
        }
        const statement = connection.prepare('CALL SHOW_RESERVATIONS(?,?)');
        const parameters = [11, '2020-12-24'];
        var results = statement.exec(parameters, function(err, results) {
            if (err) {
                return console.error(err);
            }
            console.log(util.inspect(results, { colors: false }));
            statement.drop();
            connection.disconnect(function(err) {
                if (err) {
                    return console.error(err);
                }   
            });
        });
    });
    
  3. Update the host value in connOptions.

  4. Run the app.

    node nodeQuery.js
    
    result of running app

Note the above app makes use of some of the SAP SQL Anywhere Node.js driver classes and methods. Additional details can be found at Node.js Application Programming.

In nodeQuery.js, synchronous and asynchronous queries are performed. Notice that asynchronous method calls use callback functions. See JavaScript Asynchronous Programming and Callbacks for additional details.

Log on to answer question
Step 5: Debug the application

Visual Studio Code can run and debug a Node.js application. It is a lightweight but powerful source code editor available on Windows, macOS, and Linux.

  1. If required, Download Visual Studio Code.

  2. If required, in Visual Studio Code, choose File | Add Folder to Workspace and then add the DataLakeClientsTutorial folder.

    Workspace
  3. Open the file nodeQuery.js, place a breakpoint, select Run | Start Debugging, and select Node.js.

    Notice that the debug view becomes active.

    Notice that the program stops running at the breakpoint that was set. Step through the code by pressing F10 and observe the variable values in the variables pane.

    VS Code Debugging

Congratulations! You have now created and debugged a Node.js application that connects to and queries a data lake IQ database.

Which of the following statements are true?
×

Next Steps

Back to top