如何将参数传递给spark.sql(“”“”)?

gopyfrb3  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(1022)

我想传递一个字符串到spark.sql
这是我的问题

mydf = spark.sql("SELECT * FROM MYTABLE WHERE TIMESTAMP BETWEEN '2020-04-01' AND '2020-04-08')

我想传一个日期的字符串。
我试过这个密码

val = '2020-04-08'

s"spark.sql("SELECT * FROM MYTABLE WHERE TIMESTAMP  BETWEEN $val  AND '2020-04-08'
vjhs03f7

vjhs03f71#

尝试使用python字符串格式 {} 以及 .format(val) 作为 $val 在斯卡拉。

val = '2020-04-08'

spark.sql("SELECT * FROM MYTABLE WHERE TIMESTAMP  BETWEEN {}  AND '2020-04-08'".format(val)).show()

Example: In Pyspark: ```
spark.sql("select * from tmp").show()

+----+---+

|name| id|

+----+---+

| a| 1|

| b| 2|

+----+---+

id='1'

spark.sql("select * from tmp where id={}".format(id)).show()

+----+---+

|name| id|

+----+---+

| a| 1|

+----+---+

``` In Scala: 使用 string interpolation 替换变量的值

val id=1
spark.sql(s"select * from tmp where id=$id").show()
//+----+---+
//|name| id|
//+----+---+
//|   a|  1|
//+----+---+

相关问题