变量未绑定 Spark 中的值 [英] variable not binding value in Spark

查看:24
本文介绍了变量未绑定 Spark 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在传递变量,但它没有传递值.

I am passing variable, but it is not passing value.

我在这里填充变量值.

val Temp = sqlContext.read.parquet("Tabl1.parquet")
Temp.registerTempTable("temp")

val year = sqlContext.sql("""select value  from Temp where name="YEAR"""")
year.show()

这里是 year.show() 正确的值.

here year.show() proper value.

我在下面的代码中传递参数.

I am passing the parameter here in below code.

val data = sqlContext.sql("""select count(*) from Table where Year='$year' limit 10""")
data.show()

推荐答案

year 的值是一个 Dataframe,而不是一个特定的值 (Int> 或 Long).所以当你在字符串插值中使用它时,你会得到 Dataframe.toString 的结果,这不是你可以用来比较值的东西(toString 返回数据帧模式的字符串表示).

The value year is a Dataframe, not a specific value (Int or Long). So when you use it inside a string interpolation, you get the result of Dataframe.toString, which isn't something you can use to compare values to (the toString returns a string representation of the Dataframe's schema).

如果你可以假设 year 数据框有一个 single Row 和一个 Int 类型的 column,并且您想获取该列的值 - 您可以使用 first().getAs[Int](0) 来获取该值,然后使用它来构建您的下一个查询:

If you can assume the year Dataframe has a single Row with a single column of type Int, and you want to get the value of that column - you get use first().getAs[Int](0) to get that value and then use it to construct your next query:

val year: DataFrame = sqlContext.sql("""select value from Temp where name="YEAR"""")
// get the first column of the first row:
val actualYear: Int = year.first().getAs[Int](0) 

val data = sqlContext.sql(s"select count(*) from Table where Year='$actualYear' limit 10")

如果 Temp 表中的 value 列具有不同的类型 (String, Long) - 只需替换Int 具有该类型.

If value column in Temp table has a different type (String, Long) - just replace the Int with that type.

这篇关于变量未绑定 Spark 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆