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

查看:57
本文介绍了变量未绑定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()

推荐答案

yearDataframe,而不是特定值(IntLong).因此,当在字符串插值中使用它时,会得到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列具有不同的类型(StringLong)-只需将Int替换为该类型.

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

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

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