首页 > 解决方案 > 在 pyspark SQL 语句中传递变量

问题描述

我试图在 Spark SQL 语句中插入多个变量,并在这里发现了一个类似的问题: How to pass variables in spark SQL, using python? 我的问题是如何使用作为字符串变量的多个变量列表(下面的部门变量)来做到这一点?它适用于浮点数/整数。我已经尝试了几种语法变体,但我得到一个“不匹配的输入 'From' 期望 EOF ”错误。

configs = {"lim":10,
           "codes":"A",
           "department": " 'A', 'B', 'C'", ## this is what's not working
           "salary": "100.00, 200.00"
}

df = spark.sql("""SELECT col1, col2 from table
                  WHERE employee_id IN ({department})
AND salary IN ({salary})
                 LIMIT 10
               """.format(**configs))

标签: pythonsqlapache-sparkpyspark

解决方案


尝试转义单引号:

"department": " \'A\', \'B\', \'C\'"


推荐阅读