While coding spark SQL, we quite often encounter huge SQL queries which are written/hand-picked from data warehouse world. We store them in a string and try to execute them as a Data frame or some other purposes. The multiple lines in the sql queries and single quotes enclosing values within the sql clauses cause problems to add the entire SQL as a string to a variable. One ugly solution is to create the huge query into a single line which is not readable later. Below is the solution for it :)
a typical string declaration in scala
val a="kiran"
In Scala you create multiline strings by surrounding your text with three double quotes:
val a = """This is
a multiline
String"""
A cleaner approach is to add the stripMargin method to the end of your multiline string, and begin all lines after the first line with the pipe symbol (|):
scala> val speech = """Four score and
| |seven years ago""".stripMargin
speech: String =
Four score and
seven years ago
If you don’t like using the | symbol, you can use any character you like with the stripMargin method:
scala> val speech = """Four score and
| #seven years ago""".stripMargin('#')
speech: String =
Four score and
seven years ago
Another nice feature of Scala’s multiline string syntax is that you can include single- and double-quotes without having to escape them:
scala> val a=""" This is "a multi" line 'string' syntax """
a: String = " This is "a multi" line 'string' syntax "
See below it is needed to escape if we dont use tripe quotes.
scala> val a="This is \"a multi\" line 'string' syntax "
a: String = "This is "a multi" line 'string' syntax "
Simple isn't it? Just use tripe quotes for string declaration and you will do away with the multi-line and escaping issues...!!!
a typical string declaration in scala
val a="kiran"
In Scala you create multiline strings by surrounding your text with three double quotes:
val a = """This is
a multiline
String"""
A cleaner approach is to add the stripMargin method to the end of your multiline string, and begin all lines after the first line with the pipe symbol (|):
scala> val speech = """Four score and
| |seven years ago""".stripMargin
speech: String =
Four score and
seven years ago
If you don’t like using the | symbol, you can use any character you like with the stripMargin method:
scala> val speech = """Four score and
| #seven years ago""".stripMargin('#')
speech: String =
Four score and
seven years ago
Another nice feature of Scala’s multiline string syntax is that you can include single- and double-quotes without having to escape them:
scala> val a=""" This is "a multi" line 'string' syntax """
a: String = " This is "a multi" line 'string' syntax "
See below it is needed to escape if we dont use tripe quotes.
scala> val a="This is \"a multi\" line 'string' syntax "
a: String = "This is "a multi" line 'string' syntax "
Simple isn't it? Just use tripe quotes for string declaration and you will do away with the multi-line and escaping issues...!!!
No comments:
Post a Comment