【问题标题】:org.apache.spark.sql.Row to Intorg.apache.spark.sql.Row 到 Int
【发布时间】:2014-11-28 23:28:54
【问题描述】:

我正在尝试从 spark-sql 中的 SQL 语句中获取整数。

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

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

<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 语句返回的这个整数值以供以后使用?

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    最好的文档是源代码

    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)
    

    【讨论】:

      【解决方案2】:

      这样做的原因是num_enSchemaRDD。当您对其执行collect() 时,您会得到Array[org.apache.spark.sql.Row],因此num_en.collect()(0) 会为您提供数组的第一行。

      【讨论】:

        猜你喜欢
        • 2018-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        • 2016-01-07
        • 2016-09-17
        • 1970-01-01
        相关资源
        最近更新 更多