我需要在控制台中显示一个表。
我的简单解决方案,如果你称之为“解决方案”,如下:
override def toString() = {
var res = "\n"
var counter = 1;
res += stateDb._1 + "\n"
res += " +----------------------------+\n"
res += " + State Table +\n"
res += " +----------------------------+\n"
for (entry <- stateDb._2) {
res += " | " + counter + "\t | " + entry._1 + " | " + entry._2 + " |\n"
counter += 1;
}
res += " +----------------------------+\n"
res += "\n"
res
}
我们不必争论这个
实际上,这个问题被要求提供C#,但我也想知道Scala的一个很好的解决方案。
那么在Scala中将这样一个表绘制到控制台的方式是什么(很好/很好/简单/无论如何)?
-------------------------------------------------------------------------
| Column 1 | Column 2 | Column 3 | Column 4 |
-------------------------------------------------------------------------
| | | | |
| | | | |
| | | | |
-------------------------------------------------------------------------
答案 0 :(得分:32)
我从当前的项目中提取了以下内容:
object Tabulator {
def format(table: Seq[Seq[Any]]) = table match {
case Seq() => ""
case _ =>
val sizes = for (row <- table) yield (for (cell <- row) yield if (cell == null) 0 else cell.toString.length)
val colSizes = for (col <- sizes.transpose) yield col.max
val rows = for (row <- table) yield formatRow(row, colSizes)
formatRows(rowSeparator(colSizes), rows)
}
def formatRows(rowSeparator: String, rows: Seq[String]): String = (
rowSeparator ::
rows.head ::
rowSeparator ::
rows.tail.toList :::
rowSeparator ::
List()).mkString("\n")
def formatRow(row: Seq[Any], colSizes: Seq[Int]) = {
val cells = (for ((item, size) <- row.zip(colSizes)) yield if (size == 0) "" else ("%" + size + "s").format(item))
cells.mkString("|", "|", "|")
}
def rowSeparator(colSizes: Seq[Int]) = colSizes map { "-" * _ } mkString("+", "+", "+")
}
scala> Tabulator.format(List(List("head1", "head2", "head3"), List("one", "two", "three"), List("four", "five", "six")))
res1: java.lang.String =
+-----+-----+-----+
|head1|head2|head3|
+-----+-----+-----+
| one| two|three|
| four| five| six|
+-----+-----+-----+
答案 1 :(得分:2)
标记它。我首先考虑制作一些案例对象和类,以便生成一个可以用于显示目的的标记化列表:
sealed trait TableTokens{
val width: Int
}
case class Entry(value: String) extends TableTokens{
val width = value.length
}
case object LineBreak extends TableTokens{
val width = 0
}
case object Div extends TableTokens{
val width = 1
}
那么你可以用某种行对象形成某些约束:
case class Row(contents: List[TableTokens]) extends TableTokens{
val width = contents.foldLeft(0)((x,y) => x = y.width)
}
然后,您可以以不可变的方式检查约束和类似的事情。也许创建附加表和对齐的方法......
case class Table(contents: List[TableTokens])
这意味着您可以拥有几种不同的表格变体,其风格与您的结构不同,即HTML和CSS。
答案 2 :(得分:2)
感谢Tabulator代码!
对 Spark 数据集表格打印进行了修改。
我的意思是你可以打印DataFrame内容或拉取结果集,比如
Tabulator(hiveContext.sql("SELECT * FROM stat"))
Tabulator(hiveContext.sql("SELECT * FROM stat").take(20))
第二个当然没有标题,对于DF实现,您可以设置从Spark数据框中提取多少行进行打印,是否需要标题。
/**
* Tabular representation of Spark dataset.
* Usage:
* 1. Import source to spark-shell:
* spark-shell.cmd --master local[2] --packages com.databricks:spark-csv_2.10:1.3.0 -i /path/to/Tabulator.scala
* 2. Tabulator usage:
* import org.apache.spark.sql.hive.HiveContext
* val hiveContext = new HiveContext(sc)
* val stat = hiveContext.read.format("com.databricks.spark.csv").option("header", "true").option("inferSchema", "true").option("delimiter", "\t").load("D:\\data\\stats-belablotski.tsv")
* stat.registerTempTable("stat")
* Tabulator(hiveContext.sql("SELECT * FROM stat").take(20))
* Tabulator(hiveContext.sql("SELECT * FROM stat"))
*/
object Tabulator {
def format(table: Seq[Seq[Any]], isHeaderNeeded: Boolean) : String = table match {
case Seq() => ""
case _ =>
val sizes = for (row <- table) yield (for (cell <- row) yield if (cell == null) 0 else cell.toString.length)
val colSizes = for (col <- sizes.transpose) yield col.max
val rows = for (row <- table) yield formatRow(row, colSizes)
formatRows(rowSeparator(colSizes), rows, isHeaderNeeded)
}
def formatRes(table: Array[org.apache.spark.sql.Row]): String = {
val res: Seq[Seq[Any]] = (for { r <- table } yield r.toSeq).toSeq
format(res, false)
}
def formatDf(df: org.apache.spark.sql.DataFrame, n: Int = 20, isHeaderNeeded: Boolean = true): String = {
val res: Seq[Seq[Any]] = (for { r <- df.take(n) } yield r.toSeq).toSeq
format(List(df.schema.map(_.name).toSeq) ++ res, isHeaderNeeded)
}
def apply(table: Array[org.apache.spark.sql.Row]): Unit =
println(formatRes(table))
/**
* Print DataFrame in a formatted manner.
* @param df Data frame
* @param n How many row to take for tabular printing
*/
def apply(df: org.apache.spark.sql.DataFrame, n: Int = 20, isHeaderNeeded: Boolean = true): Unit =
println(formatDf(df, n, isHeaderNeeded))
def formatRows(rowSeparator: String, rows: Seq[String], isHeaderNeeded: Boolean): String = (
rowSeparator ::
(rows.head + { if (isHeaderNeeded) "\n" + rowSeparator else "" }) ::
rows.tail.toList :::
rowSeparator ::
List()).mkString("\n")
def formatRow(row: Seq[Any], colSizes: Seq[Int]) = {
val cells = (for ((item, size) <- row.zip(colSizes)) yield if (size == 0) "" else ("%" + size + "s").format(item))
cells.mkString("|", "|", "|")
}
def rowSeparator(colSizes: Seq[Int]) = colSizes map { "-" * _ } mkString("+", "+", "+")
}
答案 3 :(得分:0)
如果您希望它更紧凑。奖励:左对齐并在两侧填充1个字符。根据邓肯·麦格雷戈(https://stackoverflow.com/a/7542476/8547501)的回答:
def formatTable(table: Seq[Seq[Any]]): String = {
if (table.isEmpty) ""
else {
// Get column widths based on the maximum cell width in each column (+2 for a one character padding on each side)
val colWidths = table.transpose.map(_.map(cell => if (cell == null) 0 else cell.toString.length).max + 2)
// Format each row
val rows = table.map(_.zip(colWidths).map { case (item, size) => (" %-" + (size - 1) + "s").format(item) }
.mkString("|", "|", "|"))
// Formatted separator row, used to separate the header and draw table borders
val separator = colWidths.map("-" * _).mkString("+", "+", "+")
// Put the table together and return
(separator +: rows.head +: separator +: rows.tail :+ separator).mkString("\n")
}
}
scala> formatTable(Seq(Seq("head1", "head2", "head3"), Seq("one", "two", "three"), Seq("four", "five", "six")))
res0: String =
+-------+-------+-------+
| head1 | head2 | head3 |
+-------+-------+-------+
| one | two | three |
| four | five | six |
+-------+-------+-------+