Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Forked from fredbenenson/redshift_credentials.r
Created November 20, 2013 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyzwitch/7566541 to your computer and use it in GitHub Desktop.
Save randyzwitch/7566541 to your computer and use it in GitHub Desktop.
# Install the Redshift R library:
# https://github.com/pingles/redshift-r
# install.packages("~/Downloads/redshift-r-master", dependencies = T, repos = NULL, type = "source")
library(redshift)
redshift <- redshift.connect("jdbc:postgresql://REDSHIFT_DB:5439/DB_NAME", "LOGIN", "PASSWORD")
# Example Query:
data <- dbGetQuery(redshift, "SELECT COUNT(*) FROM table")
# Function to use a .sql file directly, handy if you're using version control for queries:
redshift_query_from_sql <- function (query_file) {
dbGetQuery(redshift,
paste(
scan(query_file,
what = "character",
sep = '\n'
),
collapse = '\n')
)
}
# Example:
# data <- redshift_query_from_sql("file_with_sql.sql")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment