我试图从 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/