我已经阅读了该网站上的 MonetDB.R/MonetDBLite 帖子、CRAN PDF 以及 MonetDB 网站上的一些文档,并承认自己处于看似陡峭的学习曲线的底部。
MonetDBLite 文档列出了一些函数,并附有书面警告,表明它们并不是真正要直接调用的。在 R 中使用 MonetDB.R 函数时,会出现一条警告,指出该函数已被弃用,请考虑使用 MonetDBLite。
我的数据集约为 1 亿行 x 60 个因子列,目前占用 30 个已保存到 rds 文件的 data.frame。我没有足够的内存来在 R 中处理整个数据,因此我想将其全部转储到 MonetDB 中并仅提取特定任务所需的数据。
如果我选择每个 data.frame 一个表范例,什么 R 脚本将对 MonetDB 数据库中的所有表运行查询?我认为这是某种类型的 JOIN。这可以通过 CREATE VIEW 来完成吗?
如果我选择一个大表范例,什么 R 脚本将允许我附加到现有的 MonetDB 表?
任何指导将不胜感激。以下是我到目前为止能够拼凑的内容:
library( MonetDB.R )
library( MonetDBLite )
library( DBI )
library( digest )
dbDir <- 'myDirectory'
#
# Create connection to database
#
conn <- dbConnect( MonetDBLite(), dbDir )
#
# Get data.table
#
f <- choose.files( default='directory to rds files',
caption="select files for processing:",
multi=FALSE )
dt <- readRDS( file=f )
# extract file name without extension
tblName <- sub( "^([^.]*).*", "\\1", basename( f ) )
#
# Add data.table to db
#
dbWriteTable( conn, tblName, dt )
#
# Count rows
#
qryStr <- paste( "SELECT COUNT(*) FROM", tblName )
dbGetQuery( conn, qryStr )
#
# After loading a few tables, stitch together into one view
#
viewStr <- "CREATE VIEW big_tbl AS
SELECT * FROM (
SELECT 'table1' AS type, table1.* FROM table1
UNION ALL
SELECT 'table2' AS type, table2.* FROM table2
UNION ALL
SELECT 'table3' AS type, table3.* FROM table3
) AS big_tbl_table"
dbGetQuery( conn, viewStr )
# dbListTables( conn )
#[1] "big_tbl" "table1"
#[3] "table2" "table3"
#
# Get VIEW into R
#
library( dplyr )
mdb <- src_monetdb( embedded=dbDir )
bigView <- tbl( mdb, 'big_tbl' ) # Error: select is not a character vector
bigView <- tbl( mdb, 'table1' )
#
# Disconnect from db
#
dbDisconnect( conn )
最佳答案
所有小表都具有相同的架构/列吗?在这种情况下,我将使用 dbWriteTable()
将它们全部放入一个大表中,并将 append
标志设置为 TRUE
。
关于r - 一张大 table 或多张小 table ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36945635/