尝试用两列 [Seq(), String] 创建数据框 - Spark

Trying to create dataframe with two columns [Seq(), String] - Spark

当我 运行 在 spark-shell 上执行以下操作时,我得到一个数据帧:

scala> val df = Seq(Array(1,2)).toDF("a")

scala> df.show(false)
+------+
|a     |
+------+
|[1, 2]|
+------+

但是当我运行创建一个包含两列的数据框时:

scala> val df1 = Seq(Seq(Array(1,2)),"jf").toDF("a","b")
<console>:23: error: value toDF is not a member of Seq[Object]
    val df1 = Seq(Seq(Array(1,2)),"jf").toDF("a","b")

我收到错误:

Value toDF is not a member of Seq[Object].

我该怎么做? toDF 是否仅支持具有原始数据类型的序列?

要使 toDF 方法起作用,您需要 TupleSeq

val df1 = Seq((Array(1,2),"jf")).toDF("a","b")
// df1: org.apache.spark.sql.DataFrame = [a: array<int>, b: string]

df1.show
+------+---+
|     a|  b|
+------+---+
|[1, 2]| jf|
+------+---+

为更多行添加更多元组:

val df1 = Seq((Array(1,2),"jf"), (Array(2), "ab")).toDF("a","b")
// df1: org.apache.spark.sql.DataFrame = [a: array<int>, b: string]

df1.show
+------+---+
|     a|  b|
+------+---+
|[1, 2]| jf|
|   [2]| ab|
+------+---+