我在 dataframe 中有一個firstday時間戳格式的列,看起來像這樣2022-03-01 00:00:00我試圖run_date在運行時傳遞一個字串引數作為過濾器。但是下面的sql沒有用。你如何做到這一點?非常感謝!
嘗試轉換run_date,看起來像2022-03-28一個月的第一天,并用它來比較firstday
spark.sql("select * from df where firstday = date_trunc('mon','{}')".format(run_date))
uj5u.com熱心網友回復:
差不多了,格式問題很少
原始df
--- ----------
| id| firstday|
--- ----------
| 1|2022-03-01|
| 2|2022-03-17|
--- ----------
應用過濾器
run_date="'2022-03-21'"
filtereddf=spark.sql(("select * from df where firstday ==date_trunc('mon',{})").format(run_date))
filtereddf.show()
要么
run_date="2022-03-21"
filtereddf=spark.sql(("select * from df where firstday ==date_trunc('mon','{}')").format(run_date))
filtereddf.show()
--- ----------
| id| firstday|
--- ----------
| 1|2022-03-01|
--- ----------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/454998.html
標籤:Python 阿帕奇火花 日期 pyspark apache-spark-sql
上一篇:JetpackComposeTextField引數串列;顏色不允許
下一篇:對嵌套在陣列中的結構元素進行排序
