scala - org.apache.spark.sql.Row 到 Int

标签 scala apache-spark

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

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

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

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

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

最佳答案

最好的文档是源

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)

应用于您的示例:
num = num_en.collect()(0).getInt(0)

关于scala - org.apache.spark.sql.Row 到 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538220/

相关文章:

python - PySpark 根据列条件删除重复项

apache-spark - Apache Spark 是否不如 Scikit Learn 准确?

scala - Apache Spark - UDF 似乎不适用于 spark-submit

apache-spark - 在 for 循环内将映射函数附加到 PySpark RDD

apache-spark - Spark : Poor performance on distributed system. 如何改进>

csv - 将 csv 文件与不匹配的列合并

python - 重命名数据框列名称

bash - 使用scala通过ssh远程发出命令的问题

scala - 组合具有多个参数的函数

scala - 如何在Spark DataFrames/Spark SQL中使用架构读取JSON