R 在具有 dplyr 或 RPostgreSQL 的模式下访问 redshift table
R access redshift table under a schema with dplyr or RPostgreSQL
我正在尝试使用我的 mac.
连接到 Redshift 中的数据库
我设法使用 dplyr 和 RPostgreSQL 连接到 Redshift,但即使我可以看到所有可用表而不管模式如何,我也无法访问它们中的任何一个,因为它们都处于不同的模式下。
我已经尝试了各种语法来指定模式,但我没有得到任何地方。
这是我的 RPostgreSQL 代码:
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
postgre.conn <-dbConnect(drv,
host="localhost", port="XXXX", dbname="redshiftdb",
user="XXXX", password="XXXX")
dbListTables(postgre.conn)
这会列出所有表,而不考虑模式。
我可以看到特定模式下的所有表,所以这有效:
dbGetQuery(postgre.conn,
"SELECT table_name FROM information_schema.tables
WHERE table_schema='my_schema'")
但我无法使用以下任何命令从 my_schema.my_table 访问数据:
dbSendQuery(postgre.conn,"SELECT * FROM my_table LIMIT 10")
dbSendQuery(postgre.conn,"SELECT * FROM my_schema.my_table LIMIT 10")
dbSendQuery(postgre.conn,"SELECT * FROM my_table WHERE table_schema='my_schema' LIMIT 10")
dbSendQuery(postgre.conn,"SELECT * FROM c("my_schema", "my_table") LIMIT 10")
同样这是我的 dplyr 代码:
library(dplyr)
dplyr.conn <- src_postgres(host="localhost", port="XXXX",
dbname = "redshiftdb", user = "XXXX", password = "XXXX")
head(src_tbls(dplyr.conn)) # lists all the tables, regardless of schema
但是,none 这些工作:
tbl(dplyr.conn, sql("SELECT * FROM my_table LIMIT 10"))
tbl(dplyr.conn, sql("SELECT * FROM my_schema.my_table LIMIT 10"))
我也尝试在这两种情况下指定搜索路径:
dplyr.conn <- src_postgres(host="localhost", port="XXXX",
dbname = "redshiftdb", user = "XXXX", password = "XXXX",
options="-c search_path=my_schema")
postgre.conn <-dbConnect(drv,
host="localhost",
port="XXXX",
dbname="redshiftdb",
user="XXXX",
password="XXXX",
options="-c search_path=my_schema")
但这些仍然无效:
tbl(dplyr.conn, sql("SELECT * FROM my_table LIMIT 10"))
dbSendQuery(postgre.conn,"SELECT * FROM my_table LIMIT 10")
有什么想法...?
使用in_schema()
命令。代码类似于:
t <- tbl(dplyr.conn, in_schema("sheman_name", "table_name")
library(glue)
schema <- "your_schema"
tbl <- "your_table"
var <- "your_var"
conn <- "your_connection_to_database"
select_query <- glue_sql('
SELECT {`var`}
FROM {`schema`}.{`tbl`} ', .con = conn)
DBI::dbGetQuery(conn, select_query)
我正在尝试使用我的 mac.
连接到 Redshift 中的数据库
我设法使用 dplyr 和 RPostgreSQL 连接到 Redshift,但即使我可以看到所有可用表而不管模式如何,我也无法访问它们中的任何一个,因为它们都处于不同的模式下。
我已经尝试了各种语法来指定模式,但我没有得到任何地方。
这是我的 RPostgreSQL 代码:
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
postgre.conn <-dbConnect(drv,
host="localhost", port="XXXX", dbname="redshiftdb",
user="XXXX", password="XXXX")
dbListTables(postgre.conn)
这会列出所有表,而不考虑模式。
我可以看到特定模式下的所有表,所以这有效:
dbGetQuery(postgre.conn,
"SELECT table_name FROM information_schema.tables
WHERE table_schema='my_schema'")
但我无法使用以下任何命令从 my_schema.my_table 访问数据:
dbSendQuery(postgre.conn,"SELECT * FROM my_table LIMIT 10")
dbSendQuery(postgre.conn,"SELECT * FROM my_schema.my_table LIMIT 10")
dbSendQuery(postgre.conn,"SELECT * FROM my_table WHERE table_schema='my_schema' LIMIT 10")
dbSendQuery(postgre.conn,"SELECT * FROM c("my_schema", "my_table") LIMIT 10")
同样这是我的 dplyr 代码:
library(dplyr)
dplyr.conn <- src_postgres(host="localhost", port="XXXX",
dbname = "redshiftdb", user = "XXXX", password = "XXXX")
head(src_tbls(dplyr.conn)) # lists all the tables, regardless of schema
但是,none 这些工作:
tbl(dplyr.conn, sql("SELECT * FROM my_table LIMIT 10"))
tbl(dplyr.conn, sql("SELECT * FROM my_schema.my_table LIMIT 10"))
我也尝试在这两种情况下指定搜索路径:
dplyr.conn <- src_postgres(host="localhost", port="XXXX",
dbname = "redshiftdb", user = "XXXX", password = "XXXX",
options="-c search_path=my_schema")
postgre.conn <-dbConnect(drv,
host="localhost",
port="XXXX",
dbname="redshiftdb",
user="XXXX",
password="XXXX",
options="-c search_path=my_schema")
但这些仍然无效:
tbl(dplyr.conn, sql("SELECT * FROM my_table LIMIT 10"))
dbSendQuery(postgre.conn,"SELECT * FROM my_table LIMIT 10")
有什么想法...?
使用in_schema()
命令。代码类似于:
t <- tbl(dplyr.conn, in_schema("sheman_name", "table_name")
library(glue)
schema <- "your_schema"
tbl <- "your_table"
var <- "your_var"
conn <- "your_connection_to_database"
select_query <- glue_sql('
SELECT {`var`}
FROM {`schema`}.{`tbl`} ', .con = conn)
DBI::dbGetQuery(conn, select_query)