我有一个R data.frame,其中一列包含整数列表 - 即,每个元素都嵌入一整个整数列表。我想将此data.frame转换为SparkR DataFrame,尽管SparkR返回错误。
以下是创建示例R data.frame的方法:
indices <- 1:4
myDf <- data.frame(indices)
myDf$data <- list(rep(0, 20))
查看myDf
给出以下内容(正如我所料):
> str(myDf)
'data.frame': 4 obs. of 2 variables:
$ indices: int 1 2 3 4
$ data :List of 4
..$ : num 0 0 0 0 0 0 0 0 0 0 ...
..$ : num 0 0 0 0 0 0 0 0 0 0 ...
..$ : num 0 0 0 0 0 0 0 0 0 0 ...
..$ : num 0 0 0 0 0 0 0 0 0 0 ...
> head(myDf)
indices data
1 1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2 2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3 3 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4 4 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
从中创建SparkR DataFrame实际上会执行而不会出错:
library(SparkR, lib.loc=paste0(Sys.getenv("SPARK_HOME"),"/R/lib"))
sparkR.session(master = "local[*]")
mySparkDf <- as.DataFrame(myDf)
但是,mySparkDf
上的后续操作似乎不起作用:
> collect(mySparkDf)
17/07/13 17:23:00 ERROR executor.Executor: Exception in task 0.0 in stage 1.0 (TID 1)
java.lang.RuntimeException: Error while encoding: java.lang.RuntimeException:
java.lang.Double is not a valid external type for schema of array<double>
if (assertnotnull(input[0, org.apache.spark.sql.Row, true]).isNullAt) null
else validateexternaltype(getexternalrowfield(assertnotnull(input[0, org.apache.spark.sql.Row, true]), 0, indices), IntegerType) AS indices#0
... long stack trace ...
根据Spark documentation,实际上应该允许列表?即使查看Spark DataFrame的模式,也会显示该列表已成功存储为ArrayType:
> schema(mySparkDf)
StructType
|-name = "indices", type = "IntegerType", nullable = TRUE
|-name = "data", type = "ArrayType(DoubleType,true)", nullable = TRUE
使用Spark 2.2.0,R 3.4.0,Java 1.8.0_131。
有什么想法吗?在此先感谢您的帮助!
答案 0 :(得分:0)
我在执行后尝试了这段代码,在转换为sparkdataframe.ie
时遇到错误Error in structField.character(names[[i]], types[[i]], TRUE) :
Field type must be a string.
我认为myDf无法转换为sparkdataframe。 你可以像这样转换mySparkDf&lt; - as.DataFrame(myDf $ data)。 但你不会得到所需的输出。
答案 1 :(得分:0)
我认为您需要首先在R中规范化您的数据。我收到与@midhunxavier相同的错误。数组类型double是spark r中的麻烦。您是否考虑过使用R中的purr包来首先规范化您的数据?以下链接可能会有所帮助:https://jennybc.github.io/purrr-tutorial/ls13_list-columns.html