r-并行运行sparklyr函数



我目前正在使用sparklyr转换大型数据集,并根据输入的开始和结束日期计算一组度量。此函数在同一数据帧上运行多个开始和结束日期,以生成多个最终数据帧。这个函数最初是使用dplyr编写的,并使用mclapply进行了并行化,但数据已经变得非常大,所以我开始探索spark作为更好的替代方案。这对大型数据集是有效的(将处理时间从大约8小时降低到大约3小时(,但对于小型数据集,使用spark处理增加了所需的时间,主要是因为我使用for循环而不是mclapply来执行代码的并行运行。我想知道是否有一种方法可以为这些较小的数据集并行运行sparklyr函数。例如,我们有下面的事务数据帧和一组开始/结束日期,它们在for循环中运行以生成三个额外的数据帧:

transactions <- data.frame(date = c(as.Date('2022-01-01'),as.Date('2021-01-01'),as.Date('2020-01-01')),
transaction_value = c(10,20,10))
transactions_tbl <- copy_to(sc, transactions)
start_dates <- c(as.Date('2022-01-01'), as.Date('2022-01-01'), as.Date('2022-01-01'))
end_dates <- c(as.Date('2022-12-31'), as.Date('2022-12-31'), as.Date('2022-12-31'))
tot_val_func <- function(transactions_tbl,start_date, end_date) {
year_tot <- transactions_tbl %>% 
dplyr::filter(date >= start_date & date <= end_date) %>%
dplyr::summarise(total_val = sum(transaction_value)) %>%
dplyr::collect()

return(year_tot)
}
for (i in 1:length(start_dates)) {
total_val_func(transactions_tbl,start_dates[i],end_dates[i])
# Dataframe exported to a data warehouse after this transformation
}

对于输入日期集,是否可以在后端使用sparklyr并行调用total_val_func,或者该函数是否需要按照上面代码中的规定顺序运行?

根据sparklyr文档(https://cran.r-project.org/web/packages/sparklyr/sparklyr.pdf),您可以使用并行化进行交叉验证,但我不知道模型(函数(本身是并行化的,还是只是交叉验证实验。

最新更新