变量未绑定Spark中的值 [英] variable not binding value in 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
数据框具有一个单行,且该行具有单个列且类型为Int
,则您希望获取该列的值-您可以使用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屋!