Python修改以下代码,实现SQL语句的dyano为前一天的时间:def spark_sql_test(): conf = SparkConf().setAppName("test_spark_sql").setMaster("yarn").setSparkHome("/usr/local/share/spark") sc = SparkContext(conf=conf) hc = HiveContext(sc) result = hc.sql("select * from oppo_os_pso.nps_question_new_label where use_time='18月' and phone_type in ('RENO 6','RENO6 PRO','RENO6 PRO+') and dayno = '2023-04-16'") result.printSchema() result.show()
时间: 2024-02-24 08:58:32 浏览: 64
ARIMA.zip_arima_python arima_pytho实现SARIMA_时间序列_时间序列分析
可以使用Python中的datetime模块来实现日期的动态修改,修改后的代码如下:
```python
from datetime import datetime, timedelta
def spark_sql_test():
conf = SparkConf().setAppName("test_spark_sql").setMaster("yarn").setSparkHome("/usr/local/share/spark")
sc = SparkContext(conf=conf)
hc = HiveContext(sc)
dyano = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
result = hc.sql("select * from oppo_os_pso.nps_question_new_label where use_time='18月' and phone_type in ('RENO 6','RENO6 PRO','RENO6 PRO+') and dayno = '{}'".format(dyano))
result.printSchema()
result.show()
```
通过datetime.now()获取当前时间,然后减去timedelta(days=1)表示向前推一天。最后将日期格式化为'%Y-%m-%d'的形式作为SQL语句中的dayno值即可。
阅读全文