R/init_redshift.R

Defines functions init_redshift

Documented in init_redshift

#' Amazon Redshift Database Connection
#'
#' Maintains a connection to an Amazon Redshift database that you can query
#' @param factory the redshift factory name
#' @param redshift_url the Amazon S3 URL where the Redshift instance is hosted
#' @param username your username, if you do not include this R will prompt you when the object is created
#' @param password your password, if you do not include this R will prompt you when the object is created
#' @return an object that maintains a connection to the server that you can query with SQL
#' @keywords SQL Server, database, SQL
#' @export
#' @examples

init_redshift <- function(redshift_url,
                          username = NULL,
                          password = NULL){

  # PROMPT FOR CREDIENTIALS IF NEEDED

  if (is.null(username))
    username <- readline(prompt = "Enter Amazon Redshift username: ")

  if (is.null(password))
    password <- readline(prompt = "Enter Amazon Redshift password: ")

  jdbc_jar_loc <- system.file("dbr_resources",
                              "RedshiftJDBC42-1.2.10.1009.jar",
                              package = "dbr")

  library(RJDBC)

  .jinit(classpath = jdbc_jar_loc,
         parameters = c("-Xms4g", "-Xmx4g", "-d64", "-server"),
         force.init = TRUE)
  redshift_drv_name <- "com.amazon.redshift.jdbc.Driver"
  redshift_drv <- RJDBC::JDBC(redshift_drv_name, jdbc_jar_loc, identifier.quote = "\"")
  conn <- RJDBC::dbConnect(redshift_drv, redshift_url, user = username, password = password)

  conn

}
MattjCamp/Database documentation built on May 15, 2023, 9:35 p.m.