如何使用python在Spark SQL中传递变量? [英] How to pass variables in spark SQL, using python?

查看:722
本文介绍了如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆