-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-928823: Add rows vs streaming sample
- Loading branch information
1 parent
2645782
commit 3502988
Showing
4 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable no-console */ | ||
const snowflake = require('snowflake-sdk'); | ||
const { connectUsingEnv, destroyAsync } = require('./helpers'); | ||
|
||
const executeQuery = (connection, query, binds = undefined) => new Promise((resolve, reject) => { | ||
connection.execute({ | ||
sqlText: query, | ||
binds: binds, | ||
complete: function (err, stmt, rows) { | ||
if (!err) { | ||
resolve(rows); | ||
} else { | ||
reject(err); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
const executeQueryStreaming = (connection, query, binds = undefined) => new Promise((resolve, reject) => { | ||
const stmt = connection.execute({ | ||
sqlText: query, | ||
binds: binds, | ||
streamResult: true, | ||
}); | ||
stmt.streamRows() | ||
.on('error', err => reject(err)) | ||
.on('data', () => {}) | ||
.on('end', () => resolve()); | ||
}); | ||
|
||
async function runQueryReadingResultsFromRows(query){ | ||
const connection = await connectUsingEnv(); | ||
console.time('without streaming'); | ||
await executeQuery(connection, query); | ||
console.timeEnd('without streaming'); | ||
await destroyAsync(connection); | ||
} | ||
|
||
async function runQueryReadingResultsFromStream(query){ | ||
const connection = await connectUsingEnv(); | ||
console.time('with streaming'); | ||
await executeQueryStreaming(connection, query); | ||
console.timeEnd('with streaming'); | ||
await destroyAsync(connection); | ||
} | ||
|
||
async function main() { | ||
const query = 'Select 1'; // Set your query here | ||
snowflake.configure({ logLevel: 'ERROR' }); | ||
console.log(`Executing query: ${query}`); | ||
await runQueryReadingResultsFromRows(query); | ||
await runQueryReadingResultsFromStream(query); | ||
} | ||
|
||
main(); |