pass列表作为sparksql语句的参数

bzzcjhmw  于 2021-07-13  发布在  Spark
关注(0)|答案(1)|浏览(288)

我希望将list作为参数传递给sparksql语句。

process_date = '2020-01-01'

df1 = spark.sql("""select '{0}', * from table1""".format(process_date))

This works for a string. 
So df1 is created successfully.
But If I have List like this 

list1 = ['a','b','c']

df2 = spark.sql("""select '{0}','{1}',* from table1""".format(process_date,list1))

This is not working for me.
31moq8wy

31moq8wy1#

你可以用 join 和列表理解,得到下面的sql语句

"select '2020-01-01','a','b','c',* from table1"
print("""select '{0}',{1},* from table1""".format(process_date,",".join((f"'{i}'" for i in list1))))

相关问题