如何使用python在spark SQL中传递变量? [英] How to pass variables in spark SQL, using python?
本文介绍了如何使用python在spark SQL中传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用 python 编写 spark 代码.如何在 spark.sql 查询中传递变量?
I am writing spark code in python. How do I pass a variable in a spark.sql query?
q25 = 500
Q1 = spark.sql("SELECT col1 from table where col2>500 limit $q25 , 1")
目前上面的代码不起作用?我们如何传递变量?
Currently the above code does not work? How do we pass variables?
我也试过了,
Q1 = spark.sql("SELECT col1 from table where col2>500 limit q25='{}' , 1".format(q25))
推荐答案
您需要像这样删除字符串格式中的单引号和 q25
:
You need to remove single quote and q25
in string formatting like this:
Q1 = spark.sql("SELECT col1 from table where col2>500 limit {}, 1".format(q25))
更新:
基于您的新查询:
spark.sql("SELECT col1 from table where col2>500 order by col1 desc limit {}, 1".format(q25))
注意 SparkSQL 不支持 OFFSET,所以查询无法工作.
Note that the SparkSQL does not support OFFSET, so the query cannot work.
如果您需要添加多个变量,您可以尝试这种方式:
If you need add multiple variables you can try this way:
q25 = 500
var2 = 50
Q1 = spark.sql("SELECT col1 from table where col2>{0} limit {1}".format(var2,q25))
这篇关于如何使用python在spark SQL中传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文