转义 Spark SQL 的用户输入 [英] Escaping user inputs for Spark SQL

查看:22
本文介绍了转义 Spark SQL 的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用 JDBC 连接到 Spark 的 Thrift Server,并使用 JDBC 准备好的语句来逃避潜在的恶意用户输入.

I have been using a JDBC connection to Spark's Thrift Server so far and using JDBC prepared statements to escape potentially malicious user input.

我现在正在尝试将我们的代码直接移植到 HiveContext(即消除使用 Thrift Server),但我不太确定如何生成正确转义的 sql 语句...

I am trying to port our code directly to HiveContext now (i.e. eliminate the use of Thrift Server) and I am not quite sure how to generate a properly escaped sql statement...

想知道是否有人有正确的方法来做到这一点?

Wondering if someone has ideas on proper way to do this?

具体来说,我很想发表这个声明

To be concrete, I would love to issue this statement

 val df = myHiveCtxt.sql(sqlText)

但我想防范潜在的 SQL 注入.

but I would like to defend against potential SQL injection.

推荐答案

args = {
  "a": "I'm",
  "b": 1964
}
spark.udf.register("arg", lambda x: args[x]) 
 
spark.sql("select arg('a'),arg('b')").show()

诀窍是注册一个 UDF.上面的代码片段是 python,但它可以在 Scala 和其他语言中完成.

The trick is to register a UDF. The above snippet is python but it can be done in Scala and other languages.

以上是在 pyspark 中,但可以在 Scala 或其他语言中完成.诀窍是注册一个 udf.

The above is in pyspark but it can be done in Scala or other languages. The trick is to register an udf.

这篇关于转义 Spark SQL 的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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