循环过滤火花tbl

时间:2019-06-06 02:33:52

标签: r dplyr sparklyr tibble

我正在将sparklyrdplyr一起使用,以读取大小比可用RAM大得多的数据。因此,我没有将数据加载到内存中。

dat_tbl <- spark_read_parquet(sc, name = "dat", path = path, memory = FALSE) 

当我尝试以以下方式过滤(循环中的数据,说i = 1)时,它失败

var_name <- unique_values$Species[1]
res <- dat_tbl %>%
             filter(Species ==  unique_values$Species[1])
res                      
  

UseMethod(“ escape”)中的错误:     没有适用于“ c('tbl_df','tbl','data.frame')”类的对象的'escape'适用方法

但是,以下成功!

var_name <- unique_values$name[1]
res <- dat_tbl %>%
             filter(Species ==  var_name) 

我想知道为什么!?

编辑

这是一个可复制的小例子。

library(sparklyr)
library(dplyr)

# setting up
sc <- spark_connect(master = "local")
iris_tbl <- copy_to(sc, iris)
spark_write_parquet(iris_tbl, "temp")

tbl <- spark_read_parquet(sc, "data", "temp")

unique_values <- tbl %>% distinct(Species) %>% collect
unique_values$Species[1]
#[1] "versicolor"
class(unique_values$Species[1])
#[1] "character"

res <- tbl %>% filter(Species ==  unique_values$Species[1] )
res
# Error in UseMethod("escape") : 
#   no applicable method for 'escape' applied to an object of 
#   class "c('tbl_df', 'tbl', 'data.frame')"

var_name <- unique_values$Species[1]
res <- tbl %>% filter(Species ==   var_name)
res

# # Source: spark<?> [?? x 5]
#    Sepal_Length Sepal_Width Petal_Length Petal_Width Species   
#           <dbl>       <dbl>        <dbl>       <dbl> <chr>     
#  1          7           3.2          4.7         1.4 versicolor
#  2          6.4         3.2          4.5         1.5 versicolor
#  3          6.9         3.1          4.9         1.5 versicolor
#  4          5.5         2.3          4           1.3 versicolor
#  5          6.5         2.8          4.6         1.5 versicolor

1 个答案:

答案 0 :(得分:0)

尽管这不是一个完整的答案,但是在将dplyr表达式转换为SQL时出现了问题。

失败

tbl %>% filter(Species ==  unique_values$Species[1] ) %>% show_query()

#<SQL>
  

UseMethod(“ escape”)中的错误:     没有适用于“ c('tbl_df','tbl','data.frame')”类的对象的'escape'适用方法

工程

var_name <- unique_values$Species[1]
tbl %>% filter(Species ==  var_name) %>% show_query()
#<SQL>
#SELECT *
#FROM `data`
#WHERE (`Species` = "versicolor")
相关问题