org.apache.spark.sql.Row为Int [英] org.apache.spark.sql.Row to Int
问题描述
我试图让从火花SQL SQL语句的整数。
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是SchemaRDD,和num,根据错误,我得到一个行。
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
问题是,我无法找到任何有用的文档,无论是org.apache.spark.sql.Row或org.apache.spark.sql.catalyst.ex pressions.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.
我怎么能提取这一整数值,以备后用SQL语句返回?
How can I extract this one integer value that the SQL statement returns for later use?
推荐答案
最好的文档是源
<一个href=\"https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/api/java/Row.scala#L37\"相对=nofollow> 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)
这篇关于org.apache.spark.sql.Row为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!