如何在sparklyr中按名称引用Spark DataFrame并将其分配给变量? [英] How to refer to a Spark DataFrame by name in sparklyr and assign it to a variable?

查看:99
本文介绍了如何在sparklyr中按名称引用Spark DataFrame并将其分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我运行了以下代码,但我忘记了将Spark数据帧 iris 分配给R中的变量,我不能使用。 .value 进行分配,因为我在将数据复制到Spark后立即运行了一些其他代码。

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

如何将Spark数据帧 iris附加到R中的变量称为 iris_tbl

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

推荐答案

copy_to 提供其他 name 参数默认情况下,它设置为:

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

deparse(substitute(df))

所以在您的情况下,名称为 iris 。如果您想要更可预测的行为,则应该手动设置名称:

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")

然后,您可以通过<< c $ c> dplyr code> tbl :

Then you can access it dplyr way with tbl:

dplyr::tbl(sc, "foo")

或通过Spark会话:

or via Spark session:

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

所有可用于生产环境的读取器方法( copy_to 不应用作测试和开发工具,而只能用作其他工具)需要 name ,因此您可以用相同的方式引用表

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")

这篇关于如何在sparklyr中按名称引用Spark DataFrame并将其分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆