子集数据后 Sparklyr "NoSuchTableException" 错误

Sparklyr "NoSuchTableException" error after subsetting data

我是 sparklyr 的新手,没有接受过任何正式培训 - 这在这个问题之后会变得很明显。我也更偏向于统计学家,这没有帮助。子设置 Spark DataFrame.

后出现错误

考虑以下示例:

library(sparklyr)
library(dplyr)

sc <- spark_connect(master = "local[*]")
iris_tbl <- copy_to(sc, iris, name="iris", overwrite=TRUE)

#check column names
colnames(iris_tbl)

#subset so only a few variables remain  
subdf <- iris_tbl %>%
  select(Sepal_Length,Species)

subdf <- spark_dataframe(subdf)

#error happens when I try this operation
spark_session(sc) %>% 
  invoke("table", "subdf")

我得到的错误是:

Error: org.apache.spark.sql.catalyst.analysis.NoSuchTableException
        at org.apache.spark.sql.hive.client.ClientInterface$$anonfun$getTable.apply(ClientInterface.scala:122)
        at org.apache.spark.sql.hive.client.ClientInterface$$anonfun$getTable.apply(ClientInterface.scala:122)

还有其他几行错误。

我不明白为什么会出现此错误。 "subdf" 是火花 DataFrame.

要理解为什么这不起作用,您必须了解当您 copy_to 时会发生什么。在内部 sparklyr 将使用 Spark Metastore 临时注册 table 并将其或多或少地视为另一个数据库。这就是为什么:

spark_session(sc) %>% invoke("table", "iris")

可以找到"iris" table:

<jobj[32]>
  class org.apache.spark.sql.Dataset
  [Sepal_Length: double, Sepal_Width: double ... 3 more fields]
另一方面,

subdf 只是普通的本地对象。它未在 Metastore 中注册,因此无法使用 Spark 目录访问它。要使其正常工作,您可以注册 Spark DataFrame:

subdf <- iris_tbl %>% 
  select(Sepal_Length, Species)

spark_dataframe(subdf) %>%
  invoke("createOrReplaceTempView", "subdf")

copy_to如果数据小到足以由驱动程序处理:

subdf <- iris_tbl %>% 
  select(Sepal_Length, Species) %>% 
  copy_to(sc, ., name="subdf", overwrite=TRUE)

如果您使用 Spark 1.x,则应将 createOrReplaceTempView 替换为 registerTempTable