variable not binding value in Spark

2019-10-03 15:39发布

问题:

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

I populates variable value here.

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

val year = sqlContext.sql("""select value  from Temp where name="YEAR"""")
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()

回答1:

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).

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")

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