我正试图在火花中转换到下面
val forX=spark.sql(""" select * from abc where tv='Dish' and to_date(acd)>='2022-10-11 where indicator ='X' """)
val forY=spark.sql(""" select * from abc where tv='Dish' and to_date(acd)>='2022-10-11 where indicator ='Y' """)
val forZ=spark.sql(""" select * from abc where tv='Dish' and to_date(acd)>='2022-10-11 where indicator ='Z' """)
the above dataframe gives out as
val Mylist:List[String]=List("X","Y","S","I")
for (i <- Mylist)
val eindicator =spark.sql(""" select * from abc where tv="Dish" and to_date(acd)>='2022-10-11'
and indicator=${i} """)
println("#############",eindicator)它的赋值无法解析输入列中的“X”
有什么建议吗?如何传递这个$i值?
发布于 2022-10-27 10:50:09
将指示符值用单引号包装,这样SQL分析器就可以看到i是字符串文本,而不是列名。
请参阅火花壳中的以下示例。
scala> Seq(("value1", 1), ("value2", 2), ("value3", 3)).toDF("value1", "value2").createOrReplaceTempView("t")
scala> spark.sql("select * from t").show
+------+------+
|value1|value2|
+------+------+
|value1| 1|
|value2| 2|
|value3| 3|
+------+------+
scala> spark.sql("select * from t where value1 = value2").show // Show me rows with the value in the first column equal to the value in the second column.
+------+------+
|value1|value2|
+------+------+
+------+------+
scala> spark.sql("select * from t where value1 = 'value2'").show // Show me rows with the value in the first column equal to the string literal 'value2'.
+------+------+
|value1|value2|
+------+------+
|value2| 2|
+------+------+有关更多信息,请参见在SQL中单引号和双引号有什么区别?讨论。
https://stackoverflow.com/questions/74211964
复制相似问题