如何在 Zeppelin(Scala) 中将数据帧转换为 Seq

问题描述

我想在 Zeppelin 中将我的数据帧转换为 Seq。

我的数据框如下

+--+-------+-----+
|id| charid| name|
+--+-------+-----+
| 1|     a1|   ad|
| 2|     a2|  agf|
| 3|     a3|  ged|
| 4|     a4|  nom|
| 5|     a5| scal|
| 6|     a6|  tip|
| 7|     a7|  low|
+--+-------+-----+

那么我如何将其转换为 Seq,如下所示。

Seq[Long,(String,String)]

解决方法

您可以使用 collecttoSeq 转换为 Seq,确保您的数据集足够小以适合驱动程序节点

df.rdd
  .map(r => (r.getLong(0),(r.getString(1),r.getString(2))))
  .collect()
  .toSeq

df.collect
  .map(r => (r.getLong(0),r.getString(2))))
  .toSeq
,

尝试使用 maptoSeq

val result = df.select($"id".cast("long"),$"charid",$"name")
               .rdd
               .map(row => (row(0).asInstanceOf[Long],(row(1).asInstanceOf[String],row(2).asInstanceOf[String])))
               .collect
               .toSeq