python - 在 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))
解决方案
尝试转义单引号:
"department": " \'A\', \'B\', \'C\'"
推荐阅读
- vim - 使用vim,如何在txt文件中找到一行中的两个单词并将其替换为一行中的三个单词?
- jenkins - 将管道阶段限制为特定共享资源的最直接方法是什么?
- python - 如何在 Django 中加载 CSS 文件和 javascript 文件?
- python - 如何将字符串转换为日期?
- nexus3 - 如果我在 Nexus 私有存储库中的包名称与 npm 发布存储库中的包名称相同,那么在我安装时哪个优先?
- java - 在 Spring Boot 应用程序上使用 Flyway 时如何在 H2 中加载初始数据?
- php - sql_mode 适用于 php,但不适用于外部数据库管理工具
- python - 如何从我的机器人获取语音频道中的成员列表?
- python - 如何正确重新投影具有多个几何列的地理数据框?
- python - 找到每个子数组的最小值并返回这些最小值的最大值