我正在尝试使用 R 从 MySQL 数据库中并行获取数据。以下代码一一获取数据并且工作正常。但我想通过发送多个查询并将其保存到不同的变量中来加速这个过程。稍后我将合并变量内的时间序列。
library(RMySQL)
dbConnect(MySQL(), user='external', password='xxxxxxx', dbname='GMT_Minute_Data', host='xx.xx.xxx.xxx')
sqlData <-select TradeTime, Open, High, Low, Close from ad where tradetime between ‘2014-01-01’ and ‘2015-10-20’
data1= dbFetch(sqlData, n=-1)
sqlData <-select TradeTime, Open, High, Low, Close from ty where tradetime between ‘2014-01-01’ and ‘2015-10-20’
data2 = dbFetch(sqlData, n=-1)
sqlData <-select TradeTime, Open, High, Low, Close from ax where tradetime between ‘2014-01-01’ and ‘2015-10-20’
data3 = dbFetch(sqlData, n=-1)
connections <- dbListConnections(MySQL())
for(i in connections) {dbDisconnect(i)}
我尝试使用以下代码并行获取数据:
library(foreach)
library(doParallel)
library(RMySQL)
fetchData<- function(nInst, inst1, inst2, inst3, inst4, inst5, startDate, endDate, con1){
inst<-NULL
sqlData <-NULL
if(nInst==1)
inst<-inst1
else if(nInst==2)
inst<-inst2
else if(nInst==3)
inst<-inst3
else if(nInst==4)
inst<-inst4
else if(nInst==5)
inst<-inst5
sqlData <- dbSendQuery(con1, paste0('select TradeTime, Open, High, Low, Close from ', inst, ' where tradetime between \'', startDate, '\' and \'', endDate, '\'' ))
data1 = dbFetch(sqlData, n=-1)
print(head(data1))
data1
}
cluster = makeCluster(5, type = "SOCK")
registerDoParallel(cluster)
mydb <- NULL
clusterEvalQ(cluster, {
mydb <- dbConnect(MySQL(), user='external', password='xxxxxx', dbname='GMT_Minute_Data', host='xx.xx.xxx.xxx')
NULL
})
allDataList<-foreach(n =1:2, .verbose=TRUE, .packages=('RMySQL')) %dopar% {
fetchData(n, inst1, inst2, inst3, inst4, inst5, startDate, endDate, mydb)
}
stopCluster(cluster)
on.exit(dbDisconnect(mydb))
有时代码只为第一个仪器获取数据,而不是为其余的仪器获取数据。
如果有人知道解决方案,请提供帮助。
谢谢,