How to refer to a Spark DataFrame by name in sparklyr and assign it to a variable?

馋奶兔 提交于 2019-12-11 06:55:16

问题


Say I ran the following code and I forgot to assign the Spark dataframe iris to a variable in R and I can't use .Last.value to assign because I had run some other code right after copying the data to Spark.

library(sparklyr)
library(dplyr)
sc <- spark_connect(master = "local")
copy_to(sc, iris)
2+2 # ran some other code so can't use .Last.value

How do I assing the Spark dataframe "iris" to a variable in R called iris_tbl?


回答1:


copy_to provides additional name argument By default it is set to:

deparse(substitute(df))

so in your case the name will be iris. If you want more predictable behavior you should set the name manually:

copy_to(sc, iris, name = "foo")

Then you can access it dplyr way with tbl:

dplyr::tbl(sc, "foo")

or via Spark session:

sc %>% spark_session() %>% invoke("table", "foo") %>% sdf_register()

All production ready reader methods (copy_to shouldn't be used as anything else than a testing and development tool) require name, so you can reference tables the same way

spark_read_csv(sc, "bar", path)
tbl(sc, "bar")


来源:https://stackoverflow.com/questions/51867877/how-to-refer-to-a-spark-dataframe-by-name-in-sparklyr-and-assign-it-to-a-variabl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!