Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.apache.spark.sql.Row to Int

I'm trying to get an Integer from a SQL statement in spark-sql.

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

num_en is a SchemaRDD, and num, according to the error I get, a "Row".

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

The problem is that I can't find any useful documentation for either org.apache.spark.sql.Row or org.apache.spark.sql.catalyst.expressions.Row.

How can I extract this one integer value that the SQL statement returns for later use?

like image 742
helm Avatar asked Mar 20 '23 04:03

helm


1 Answers

The best doc is the source

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)

Applied to your example:

num = num_en.collect()(0).getInt(0)
like image 153
maasg Avatar answered Mar 28 '23 18:03

maasg