使用 dbplyr 跨数据库连接
Joining across databases with dbplyr
我正在使用数据库 tables 和 dbplyr
我有一个本地 table 并想将其与数据库上的大型(150m 行)table 连接起来
数据库PRODUCTION
是只读的
# Set up the connection and point to the table
library(odbc); library(dbplyr)
my_conn_string <- paste("Driver={Teradata};DBCName=teradata2690;DATABASE=PRODUCTION;UID=",
t2690_username,";PWD=",t2690_password, sep="")
t2690 <- dbConnect(odbc::odbc(), .connection_string=my_conn_string)
order_line <- tbl(t2690, "order_line") #150m rows
我也有一个本地的table,我们就叫它订单吧
# fill df with random data
orders <- data.frame(matrix(rexp(50), nrow = 100000, ncol = 5))
names(orders) <- c("customer_id", paste0(rep("variable_", 4), 1:4))
假设我想加入这两个 tables,我收到以下错误:
complete_orders <- orders %>% left_join(order_line)
> Error: `x` and `y` must share the same src, set `copy` = TRUE (may be slow)
问题是,如果我设置 copy = TRUE
,它会尝试下载整个 order_line
,我的计算机很快就会 运行 内存不足
另一种选择是将 orders
table 上传到数据库。这里的问题是 PRODUCTION
数据库是只读的——我必须上传到不同的数据库。尝试在 dbplyr 中跨数据库复制会导致相同的错误。
我找到的唯一解决方案是上传到 writable 数据库并使用 sql 加入它们,这远非理想
我找到了答案,您可以使用 tbl 指针中的 in_schema()
函数在同一连接中跨模式工作
# Connect without specifying a database
my_conn_string <- paste("Driver={Teradata};DBCName=teradata2690;UID=",
t2690_username,";PWD=",t2690_password, sep="")
# Upload the local table to the TEMP db then point to it
orders <- tbl(t2690, in_schema("TEMP", "orders"))
order_line <- tbl(t2690, in_schema("PRODUCTION", "order_line"))
complete_orders <- orders %>% left_join(order_line)
Another option could be to upload the orders
table to the database. The issue here is that the PRODUCTION
database is read only - I would have to upload to a different database. Trying to copy across databases in dbplyr
results in the same error.
在您的用例中,(根据已接受的答案)您的数据库似乎位于同一台服务器上,这只是使用 in_schema
的问题。如果不是这种情况,另一种方法是给定 ,它实际上提供了一个适用于只读连接的 copy_to
版本。
我正在使用数据库 tables 和 dbplyr
我有一个本地 table 并想将其与数据库上的大型(150m 行)table 连接起来
数据库PRODUCTION
是只读的
# Set up the connection and point to the table
library(odbc); library(dbplyr)
my_conn_string <- paste("Driver={Teradata};DBCName=teradata2690;DATABASE=PRODUCTION;UID=",
t2690_username,";PWD=",t2690_password, sep="")
t2690 <- dbConnect(odbc::odbc(), .connection_string=my_conn_string)
order_line <- tbl(t2690, "order_line") #150m rows
我也有一个本地的table,我们就叫它订单吧
# fill df with random data
orders <- data.frame(matrix(rexp(50), nrow = 100000, ncol = 5))
names(orders) <- c("customer_id", paste0(rep("variable_", 4), 1:4))
假设我想加入这两个 tables,我收到以下错误:
complete_orders <- orders %>% left_join(order_line)
> Error: `x` and `y` must share the same src, set `copy` = TRUE (may be slow)
问题是,如果我设置 copy = TRUE
,它会尝试下载整个 order_line
,我的计算机很快就会 运行 内存不足
另一种选择是将 orders
table 上传到数据库。这里的问题是 PRODUCTION
数据库是只读的——我必须上传到不同的数据库。尝试在 dbplyr 中跨数据库复制会导致相同的错误。
我找到的唯一解决方案是上传到 writable 数据库并使用 sql 加入它们,这远非理想
我找到了答案,您可以使用 tbl 指针中的 in_schema()
函数在同一连接中跨模式工作
# Connect without specifying a database
my_conn_string <- paste("Driver={Teradata};DBCName=teradata2690;UID=",
t2690_username,";PWD=",t2690_password, sep="")
# Upload the local table to the TEMP db then point to it
orders <- tbl(t2690, in_schema("TEMP", "orders"))
order_line <- tbl(t2690, in_schema("PRODUCTION", "order_line"))
complete_orders <- orders %>% left_join(order_line)
Another option could be to upload the
orders
table to the database. The issue here is that thePRODUCTION
database is read only - I would have to upload to a different database. Trying to copy across databases indbplyr
results in the same error.
在您的用例中,(根据已接受的答案)您的数据库似乎位于同一台服务器上,这只是使用 in_schema
的问题。如果不是这种情况,另一种方法是给定 copy_to
版本。