org.apache.spark.sql.Row 到 Int

hel*_*elm 4 scala apache-spark

我试图从 spark-sql 中的 SQL 语句中获取一个整数。

var num_en = ctx.sql("SELECT count(*) FROM table WHERE lang = 'en'")
num = num_en.collect()(0)
Run Code Online (Sandbox Code Playgroud)

num_en 是一个 SchemaRDD,而 num,根据我得到的错误,是一个“行”。

<console>:144: error: type mismatch;
 found   : org.apache.spark.sql.Row
    (which expands to)  org.apache.spark.sql.catalyst.expressions.Row
Run Code Online (Sandbox Code Playgroud)

问题是我找不到 org.apache.spark.sql.Row 或 org.apache.spark.sql.catalyst.expressions.Row 的任何有用文档。

如何提取 SQL 语句返回的这个整数值供以后使用?

maa*_*asg 7

最好的文档是源

Row.scala

  /**
   * Returns the value of column `i` as an int.  This function will throw an exception if the value
   * is at `i` is not an integer, or if it is null.
   */
  def getInt(i: Int): Int =
    row.getInt(i)
Run Code Online (Sandbox Code Playgroud)

应用于您的示例:

num = num_en.collect()(0).getInt(0)
Run Code Online (Sandbox Code Playgroud)