Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute multi line sql in spark sql

How can I execute lengthy, multiline Hive Queries in Spark SQL? Like query below:

val sqlContext = new HiveContext (sc)
val result = sqlContext.sql ("
 select ...
 from ...
");
like image 670
user2910372 Avatar asked Nov 24 '16 13:11

user2910372


People also ask

How do you write multiple lines in spark shell?

In the Spark shell you can wrap your multiple line Spark code in parenthesis to execute the code. Wrapping in parenthesis will allow you to copy multiple line Spark code into the shell or write multiple line code line-by-line.

How do you do multiple lines in SQL?

Multi-line comments start with /* and end with */ . Any text between /* and */ will be ignored.

What is multiline option in PySpark?

Solution: PySpark JSON data source API provides the multiline option to read records from multiple lines. By default, PySpark considers every record in a JSON file as a fully qualified record in a single line.


3 Answers

Use """ instead, so for example

val results = sqlContext.sql ("""
     select ....
     from ....
""");

or, if you want to format code, use:

val results = sqlContext.sql ("""
     |select ....
     |from ....
""".stripMargin);
like image 85
T. Gawęda Avatar answered Oct 14 '22 03:10

T. Gawęda


You can use triple-quotes at the start/end of the SQL code or a backslash at the end of each line.

val results = sqlContext.sql ("""
    create table enta.scd_fullfilled_entitlement as
    select *
    from my_table
    """);

results = sqlContext.sql (" \
    create table enta.scd_fullfilled_entitlement as \
    select * \
    from my_table \
    ")
like image 23
Ricardo Avatar answered Oct 14 '22 03:10

Ricardo


val query = """(SELECT
      a.AcctBranchName,
    c.CustomerNum,
    c.SourceCustomerId,
    a.SourceAccountId,
    a.AccountNum,
    c.FullName,
    c.LastName,
    c.BirthDate,
    a.Balance,
    case when [RollOverStatus] = 'Y' then 'Yes' Else 'No' end as RollOverStatus
    FROM
    v_Account AS a left join v_Customer AS c
      ON c.CustomerID = a.CustomerID AND c.Businessdate = a.Businessdate
    WHERE
    a.Category = 'Deposit' AND
    c.Businessdate= '2018-11-28'  AND
    isnull(a.Classification,'N/A') IN ('Contractual Account','Non-Term Deposit','Term Deposit')
    AND IsActive = 'Yes' ) tmp """
like image 1
kn3l Avatar answered Oct 14 '22 02:10

kn3l