如何在Zeppelin / Spark / Scala中打印数据框?

时间:2017-07-06 10:38:58

标签: scala apache-spark apache-zeppelin

我在Zeppelin 0.7笔记本中使用Spark 2和Scala 2.11。我有一个数据帧,我可以这样打印:

dfLemma.select("text", "lemma").show(20,false)

,输出如下:

+---------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|text                                                                                                                       |lemma                                                                                                                                                                  |
+---------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|RT @Dope_Promo: When you and your crew beat your high scores on FUGLY FROG  https://time.com/Sxp3Onz1w8                    |[rt, @dope_promo, :, when, you, and, you, crew, beat, you, high, score, on, FUGLY, FROG, https://time.com/sxp3onz1w8]                                                      |
|RT @axolROSE: Did yall just call Kermit the frog a lizard?  https://time.com/wDAEAEr1Ay                                        |[rt, @axolrose, :, do, yall, just, call, Kermit, the, frog, a, lizard, ?, https://time.com/wdaeaer1ay]                                                                     |

我试图通过以下方式在Zeppelin中使输出更好:

val printcols= dfLemma.select("text", "lemma")
println("%table " + printcols)

给出了这个输出:

printcols: org.apache.spark.sql.DataFrame = [text: string, lemma: array<string>]

和一个新的空白的Zeppelin段落为

[text: string, lemma: array]

有没有办法让数据框显示为格式良好的表格? TIA!

2 个答案:

答案 0 :(得分:43)

在Zeppelin中,您可以使用z.show(df)来显示漂亮的桌子。这是一个例子:

val df = Seq(
  (1,1,1), (2,2,2), (3,3,3)
).toDF("first_column", "second_column", "third_column")

z.show(df)

enter image description here

答案 1 :(得分:1)

我知道这是一个旧线程,但以防万一它有帮助...

下面是我可以展示部分 df 的唯一方法。尝试按照评论中的建议向 .show() 添加第二个参数会引发错误。

z.show(df.limit(10))