【发布时间】:2017-11-05 03:04:51
【问题描述】:
我想使用dplyr 和RMySQL 来处理我的大数据。 dplyr 代码没有问题。问题(我认为)是关于将数据从MySQL 导出到R。每次即使我在collect 中使用n=Inf,我的连接也会断开。从理论上讲,我的数据应该有超过 50K 的行,但我只能得到大约 15K。任何建议表示赞赏。
方法一
library(dplyr)
library(RMySQL)
# Connect to a database and select a table
my_db <- src_mysql(dbname='aermod_1', host = "localhost", user = "root", password = "")
my_tbl <- tbl(my_db, "db_table")
out_summary_station_raw <- select(my_tbl, -c(X, Y, AVERAGE_CONC))
out_station_mean_local <- collect(out_summary_station_raw)
方法二:使用Pool
library(pool)
library(RMySQL)
library(dplyr)
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "aermod_1",
host = "localhost",
username = "root",
password = ""
)
out_summary_station_raw <- src_pool(pool) %>% tbl("aermod_final") %>% select(-c(X, Y, AVERAGE_CONC))
out_station_mean_local <- collect(out_summary_station_raw, n = Inf)
警告信息(两种方法):
Warning messages:
1: In dbFetch(res, n) : error while fetching rows
2: Only first 15,549 results retrieved. Use n = Inf to retrieve all.
更新:
检查了日志,从服务器端看起来很好。在我的示例中,slow-log 表示 Query_time: 79.348351 Lock_time: 0.000000 Rows_sent: 15552 Rows_examined: 16449696,但 collect 无法检索完整数据。我可以使用MySQL Bench 复制相同的动作。
【问题讨论】:
-
您确定这是 R 问题而不是 MySQL 问题吗?
-
@ssdecontrol,这是可能的。或者更准确地说,如何通过 R 配置几个 MySQL 参数?例如,我在
Workbench中增加了DBMS connection read time out,但不确定通过R 的等效方式是什么。 -
我认为配置客户端超时的唯一方法是更改
my.cnf文件中的相关选项。 -
@ssdecontrol,感谢您的建议。将
interactive_timeout = 60000 wait_timeout = 60000 net_read_timeout = 60000 connect_timeout = 60000添加到my.ini并验证这些变量正在使用中。但仍然可以获得完整的数据... -
好的,太好了。您还必须考虑 server 端超时(如果有)。也许您可以检查来自
mysqld的调试输出以获取线索。否则,这个问题似乎无法回答,因为我从来没有遇到过这个问题,也不知道如何轻松地重现它。