Snowflake Node Query not showing Results

Viewed 796

I have been trying to run a simple Snowflake query using Node. Everything connects and seems ok but I can't seem to hook the Results of the query. The code below is based on the official docs here This correctly logs the number of rows but can't log the actual results. Unless i'm missing something the Docs don't show any object information.

Any ideas folks?

var snowflake = require('snowflake-sdk');
            
            var connection = snowflake.createConnection( {
            account: 'xxx',
            username: 'xxx',
            password: 'xxx',
            database: 'xxx',
            warehouse: 'xxx',
            role: 'xxx'
            } );
            connection.connect( 
            function(err, conn) {
            if (err) {
            console.error('Unable to connect: ' + err.message);
            } 
            else {
            console.log('Successfully connected to Snowflake.');
            connection_ID = conn.getId();           
            var statement = connection.execute({
            sqlText: 'SELECT * FROM MY_SCHEMA.MY_TABLE LIMIT 1;',
            
            complete: function(err, stmt, rows) {
            if (err) {
            console.error('Failed to execute statement due to the following error: ' + err.message);
            } else {
            console.log('Successfully executed statement: ' + stmt.getSqlText() + ' > ' + rows.length);
            }
            }
            });
2 Answers

Are you wanting the data to show in the terminal?

For example, instead of the SQL statement:

console.log('Successfully executed statement: ' + stmt.getSqlText() + ' > ' + rows.length)

are you looking for:

console.log(rows);

This is very basic and there are a lot of different ways to achieve what you are looking for, but I hope it helps. The Snowflake documentation is meant to help get connected with Node and send some basic information, but I am sure your Node/JavaScript development team will be the main folks to code a client-side solution to complement your workflow.

Your code seems look ok , you missing some of the step after getting stmt in the else part, the below code will help you to get the result.

      var snowflake = require('snowflake-sdk');
        
        var connection = snowflake.createConnection( {
        account: 'xxx',
        username: 'xxx',
        password: 'xxx',
        database: 'xxx',
        warehouse: 'xxx',
        role: 'xxx'
        } );
        connection.connect( 
        function(err, conn) {
        if (err) {
        console.error('Unable to connect: ' + err.message);
        } 
        else {
        console.log('Successfully connected to Snowflake.');
        connection_ID = conn.getId();
        query="select * from yourTable";
        dbConnection.execute({
                sqlText: query,
                streamResult: true,
                complete: function (error, stmt, rows) {
                    if (error) {
                        console.log(error);
                    }
                    stmt.streamRows({
                        start: 0,
                        end: stmt.getNumRows() - 1,
                    })
                        .on('error', function (error) {
                            if (error) {
                                next({ statusCode: 400, message: error });
                            }
                            next({ statusCode: 400, message: 'Unable to consume requested rows' });
                        })
                        .on('data', function (row) {
                            output.push({ name: row.name })
                        })
                        .on('end', function () {
                        });
                }
            });
            }
Related