问题描述
我不敢相信我在问这个但是...
您如何使用 SCALA 在 SPARK SQL 中转义 SQL 查询字符串?
我已经厌倦了一切,到处寻找.我认为 apache commons 库会做到这一点,但没有运气:
import org.apache.commons.lang.StringEscapeUtilsvar sql = StringEscapeUtils.escapeSql("'Ulmus_minor_'Toledo'");df.filter("topic = '" + sql + "'").map(_.getValuesMap[Any](List("hits","date"))).collect().foreach(println);
返回以下内容:
topic = '''Ulmus_minor_''托莱多'''^ at scala.sys.package$.error(package.scala:27) at org.apache.spark.sql.catalyst.SqlParser.parseExpression(SqlParser.scala:45)在 org.apache.spark.sql.DataFrame.filter(DataFrame.scala:651) 在$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.(:29) at$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.(:34) at$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.(:36) at$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.(:38) at$iwC$$iwC$$iwC$$iwC$$iwC.(:40) at$iwC$$iwC$$iwC$$iwC.(:42) at$iwC$$iwC$$iwC.(:44) 在 $iwC$$iwC.(:46)在 $iwC.(:48) 在 (:50) 在.(:54) 在 .() 在.(:7) 在 .() 在 $print()在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:497) 在org.apache.spark.repl.SparkIMain$ReadEvalPrint.call(SparkIMain.scala:1065)在org.apache.spark.repl.SparkIMain$Request.loadAndRun(SparkIMain.scala:1338)在org.apache.spark.repl.SparkIMain.loadAndRunReq$1(SparkIMain.scala:840)在 org.apache.spark.repl.SparkIMain.interpret(SparkIMain.scala:871)在 org.apache.spark.repl.SparkIMain.interpret(SparkIMain.scala:819)在org.apache.spark.repl.SparkILoop.reallyInterpret$1(SparkILoop.scala:857)在org.apache.spark.repl.SparkILoop.interpretStartingWith(SparkILoop.scala:902)在 org.apache.spark.repl.SparkILoop.command(SparkILoop.scala:814) 在org.apache.spark.repl.SparkILoop.processLine$1(SparkILoop.scala:657)在 org.apache.spark.repl.SparkILoop.innerLoop$1(SparkILoop.scala:665)在org.apache.spark.repl.SparkILoop.org$apache$spark$repl$SparkILoop$$loop(SparkILoop.scala:670)在org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply$mcZ$sp(SparkILoop.scala:997)在org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply(SparkILoop.scala:945)在org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply(SparkILoop.scala:945)在scala.tools.nsc.util.ScalaClassLoader$. SavingContextLoader(ScalaClassLoader.scala:135)在org.apache.spark.repl.SparkILoop.org$apache$spark$repl$SparkILoop$$process(SparkILoop.scala:945)在 org.apache.spark.repl.SparkILoop.process(SparkILoop.scala:1059)在 org.apache.spark.repl.Main$.main(Main.scala:31) 在org.apache.spark.repl.Main.main(Main.scala) 在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:497) 在org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:665)在org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:170)在 org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:193)在 org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:112)在 org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
帮助会很棒.
j
这可能令人惊讶,但:
var sql = "'Ulmus_minor_'Toledo'"df.filter(s"""topic = "$sql"""")
工作得很好,虽然使用它会更干净:
df.filter($"topic" sql)
I can't believe I am asking this but...
HOW DO YOU ESCAPE A SQL QUERY STRING IN SPARK SQL USING SCALA?
I have tired everything and searched everywhere. I thought the apache commons library would do it but no luck:
returns the following:
Help would be great.
j
It may be surprising but:
var sql = "'Ulmus_minor_'Toledo'"
df.filter(s"""topic = "$sql"""")
works just fine, although it would be much cleaner to use this:
df.filter($"topic" <=> sql)
这篇关于Spark SQL - 转义查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!