Last active
November 27, 2018 15:46
-
-
Save lightningspirit/c8d4c5171ab2493b68beac5d694b294e to your computer and use it in GitHub Desktop.
Redshift Unload query generator from SQL statement (warning: trusts input data)
This file contains hidden or 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
#!/usr/bin/env node | |
const fs = require('fs') | |
const usage = () => { | |
console.log('Usage: unload <aws-arn> <sql-statement-filepath> <name-of-file-in-s3>') | |
} | |
if (process.argv.length < 5 || process.argv[2] == '-h') { | |
usage() | |
process.exit(1) | |
} | |
const arn = process.argv[2] | |
const sqlFilePath = process.argv[3] | |
const s3FileName = process.argv[4] | |
const SQL = fs.readFileSync(sqlFilePath) | |
if (SQL.toString().trim() == '') { | |
console.log('Error: SQL file input is empty') | |
usage() | |
} | |
const unload = ` | |
UNLOAD ('${SQL.toString().replace(/'/g, "\\'")}') | |
TO 's3://${s3FileName}/' | |
IAM_ROLE '${arn}' | |
HEADER | |
DELIMITER AS ',' | |
ALLOWOVERWRITE | |
MAXFILESIZE AS 5 MB | |
MANIFEST; | |
` | |
console.log(unload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment