How can I pretty print a wrappedarray in Zeppelin/Spark/Scala?

拈花ヽ惹草 提交于 2019-12-10 11:36:26

问题


In this question I was told how to print a dataframe using zeppelin's z.show command. This works well except for 'WrappedArray' appearing in the lemma column:

I have tried this:

z.show(dfLemma.select(concat_ws(",", $"lemma")))

but it just gave me a list of words, not nicely formatted and I also want the racist column in my output. Any help is much appreciated.


回答1:


Here's a suggestion for formatting your array column:

import org.apache.spark.sql.Column
import org.apache.spark.sql.functions._
import sqlContext.implicits._

val df = Seq(
  (1, Array("An", "Array")), (2, Array("Another", "Array"))
).toDF("first", "second")

def formatArrayColumn(arrayColumn: Column): Column = {
  concat(lit("["), concat_ws(", ", arrayColumn), lit("]")).as(s"format(${arrayColumn.expr})")
}

val result = df.withColumn("second", formatArrayColumn($"second"))

z.show(result)

Which results in:



来源:https://stackoverflow.com/questions/45026919/how-can-i-pretty-print-a-wrappedarray-in-zeppelin-spark-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!