使用AWS驱动程序与Redshift的R连接不起作用,但与Postgre驱动程序一起使用

时间:2015-11-16 21:56:57

标签: r postgresql amazon-redshift

在按照AWS https://blogs.aws.amazon.com/bigdata/post/Tx1G8828SPGX3PK/Connecting-R-with-Amazon-Redshift提供的示例后,我正在尝试建立与我的redshift数据库的连接。但是,尝试使用推荐的驱动程序建立连接时出错。但是,当我使用Postgre驱动程序时,我可以建立与redshift DB的连接。

AWS表示他们的驱动程序“针对性能和内存管理进行了优化”,所以我宁愿使用它。有人可以在下面查看我的代码,并告诉我他们是否看错了吗?我怀疑我没有正确设置URL,但不确定我应该使用什么?在此先感谢您的帮助。

#' This code attempts to establish a connection to redshift database.  It
#' attempts to establish a connection using the suggested redshift but doesn't
#' work.

## Clear up space and set working directory

#Clear Variables
rm(list=ls(all=TRUE))
gc()

## Libriries for analyis

library(RJDBC)
library(RPostgreSQL)

#Create DBI driver for working with redshift driver directly

# download Amazon Redshift JDBC driver
download.file('http://s3.amazonaws.com/redshift-downloads/drivers/RedshiftJDBC41-1.1.9.1009.jar',
              'RedshiftJDBC41-1.1.9.1009.jar')

# connect to Amazon Redshift using specific driver        
driver_redshift <- JDBC("com.amazon.redshift.jdbc41.Driver",
               "RedshiftJDBC41-1.1.9.1009.jar", identifier.quote="`")

## Using postgre connection that works 

#postgre driver
driver_postgre <- dbDriver("PostgreSQL")

#establish connection
conn_postgre <- dbConnect(driver_postgre, host="nhdev.c6htwjfdocsl.us-west-2.redshift.amazonaws.com",
                 port="5439",dbname="dev", 
                 user="xxxx", password="xxxx")

#list the tables available
tables = dbListTables(conn_postgre)

## Use URL option to establish connection like the example on AWS website

# url <- "<JDBCURL>:<PORT>/<DBNAME>?user=<USER>&password=<PW>
# url <- "jdbc:redshift://demo.ckffhmu2rolb.eu-west-1.redshift.amazonaws.com
# :5439/demo?user=XXX&password=XXX" #useses example from AWS instructions

#url using my redshift database
url <- "jdbc:redshift://nhdev.c6htwjfdocsl.us-west-2.redshift.amazonaws.com
:5439/dev?user=xxxx&password=xxxx"

#attempt connect but gives an error
conn_redshift <- dbConnect(driver_redshift, url)

#gives the following error:
# Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1],  : 
#                   java.sql.SQLException: Error message not found: CONN_GENERAL_ERR. Can't find bundle for base name com.amazon.redshift.core.messages, locale en

## Similier to postgre example that works but doesn't work when using redshift specific driver

#gives an error saying url is missing, but I am not sure which url to use?
conn <- dbConnect(driver_redshift, host="nhdev.c6htwjfdocsl.us-west-2.redshift.amazonaws.com",
                  port="5439",dbname="dev", 
                  user="xxxx", password="xxxx")

# gives the following error: 
#Error in .jcall("java/sql/DriverManager", "Ljava/sql/Connection;", "getConnection",  : 
# argument "url" is missing, with no default

1 个答案:

答案 0 :(得分:0)

我这样做对我有用:

drv <- JDBC("com.amazon.redshift.jdbc41.Driver","PathTO/RedshiftJDBC41-1.1.2.0002.jar")
conn <- dbConnect(drv,"jdbc:redshift://......redshift.amazonaws.com:5439/dev",User,PWD)

我在你看到的不同之处是你没有在driver_redshift中提到redshift jar的完整路径。

希望它有效。