cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I escape a query string in Spark SQL?

johnmcauley
New Contributor II

Hey all,

I am trying to filter on a string but the string has a single quote - how do I escape the string in Scala?

I have tried an old version of StringEscapeUtils but no luck.

Sorry if a silly question - new to Scala.

import org.apache.commons.lang.StringEscapeUtils

var sql = StringEscapeUtils.escapeSql("'Ulmus_minor_'Toledo'");

df.filter("topic = '" + sql + "'").map(.getValuesMap[Any](List("hits","date"))).collect().foreach(println);

RETURNS:

topics = 'Ulmus_minor'Toledo' ^

 at scala.sys.package$.error(package.scala:27)

 at org.apache.spark.sql.catalyst.SqlParser.parseExpression(SqlParser.scala:45)

 at org.apache.spark.sql.DataFrame.filter(DataFrame.scala:651)

 at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:26)

 at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:31)

 at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:33)

 at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:35)

 at $iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:37)

 at $iwC$$iwC$$iwC$$iwC.<init>(<console>:39)

 at $iwC$$iwC$$iwC.<init>(<console>:41)

 at $iwC$$iwC.<init>(<console>:43)

 at $iwC.<init>(<console>:45)

 at <init>(<console>:47)

 at .<init>(<console>:51)

 at .<clinit>(<console>)

 at .<init>(<console>:7)

 at .<clinit>(<console>)

 at $print(<console>)

 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

 at java.lang.reflect.Method.invoke(Method.java:497)

 at org.apache.spark.repl.SparkIMain$ReadEvalPrint.call(SparkIMain.scala:1065)

 at org.apache.spark.repl.SparkIMain$Request.loadAndRun(SparkIMain.scala:1338)

 at org.apache.spark.repl.SparkIMain.loadAndRunReq$1(SparkIMain.scala:840)

 at org.apache.spark.repl.SparkIMain.interpret(SparkIMain.scala:871)

 at org.apache.spark.repl.SparkIMain.interpret(SparkIMain.scala:819)

 at org.apache.spark.repl.SparkILoop.reallyInterpret$1(SparkILoop.scala:857)

 at org.apache.spark.repl.SparkILoop.interpretStartingWith(SparkILoop.scala:902)

 at org.apache.spark.repl.SparkILoop.command(SparkILoop.scala:814)

 at org.apache.spark.repl.SparkILoop.processLine$1(SparkILoop.scala:657)

 at org.apache.spark.repl.SparkILoop.innerLoop$1(SparkILoop.scala:665)

 at org.apache.spark.repl.SparkILoop.org$apache$spark$repl$SparkILoop$$loop(SparkILoop.scala:670)

 at org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply$mcZ$sp(SparkILoop.scala:997)

 at org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply(SparkILoop.scala:945)

 at org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply(SparkILoop.scala:945)

 at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)

 at org.apache.spark.repl.SparkILoop.org$apache$spark$repl$SparkILoop$$process(SparkILoop.scala:945)

 at org.apache.spark.repl.SparkILoop.process(SparkILoop.scala:1059)

 at org.apache.spark.repl.Main$.main(Main.scala:31)

 at org.apache.spark.repl.Main.main(Main.scala)

 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

 at java.lang.reflect.Method.invoke(Method.java:497)

 at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:665)

 at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:170)

 at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:193)

 at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:112)

 at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)

2 REPLIES 2

johnmcauley
New Contributor II

Thanks to ZERO323 on SO - http://stackoverflow.com/questions/31969047/spark-sql-escape-query-string/31970069#31970069

df.filter($"topic"<=> sql)

Laters,

j

antoniosarco
New Contributor II

generally when u deal with apostrophe u replace the the single quote(') with ('').

More about....handling single quotes

Antonio

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group