scala 如何在spark sql中执行多行sql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40787972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute multi line sql in spark sql
提问by user2910372
How can I execute lengthy, multiline Hive Queries in Spark SQL? Like query below:
如何在 Spark SQL 中执行冗长的多行 Hive 查询?像下面的查询:
val sqlContext = new HiveContext (sc)
val result = sqlContext.sql ("
select ...
from ...
");
回答by T. Gaw?da
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);
回答by Ricardo
You can use triple-quotes at the start/end of the SQL code or a backslash at the end of each line.
您可以在 SQL 代码的开头/结尾使用三引号或在每行结尾使用反斜杠。
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 \
")
回答by vikash gupta
In addition to the above ways, you can use the below-mentioned way as well:
除了上述方式,您还可以使用下面提到的方式:
val results = sqlContext.sql("select .... " +
" from .... " +
" where .... " +
" group by ....
");
回答by kn3l
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 """
回答by Assaf Mendelson
It is worth noting that the length is not the issue, just the writing. For this you can use """ as Gaweda suggested or simply use a string variable, e.g. by building it with string builder. For example:
值得注意的是,长度不是问题,只是写作。为此,您可以使用 Gaweda 建议的 """ 或简单地使用字符串变量,例如通过使用字符串构建器构建它。例如:
val selectElements = Seq("a","b","c")
val builder = StringBuilder.newBuilder
builder.append("select ")
builder.append(selectElements.mkString(","))
builder.append(" where d<10")
val results = sqlContext.sql(builder.toString())

