YD's blog

Posted 四 14 4月 2016

R2Redshift

由R接Redshift的實作。

if (!require(RJDBC)) install.packages("RJDBC")
devtools::install_github("pingles/redshift-r")#api:https://github.com/pingles/redshift-r
library(redshift)

#Open connection
conn <- redshift.connect("jdbc:postgresql://.ap-northeast-1.redshift.amazonaws.com:5439/", "adminName", "password")  

#Set all colNames for getting the intact SQL table  
cols <- redshift.columns(conn,tableName = "")#columnNames  

#Query data with SQL syntax
data<- dbGetQuery(conn, paste("SELECT * FROM  where date_part(hour,datetimeclient)=15 AND convert(varchar(10),datetimeclient)='2016-03-04' AND city_name!='missing' AND mouseposx>0 AND mouseposy>0",sep=''))  

#Close connection
redshift.disconnect(conn)  

Category: R
Tags: R