【发布时间】:2018-12-25 14:23:28
【问题描述】:
如何在 Spark SQL 中执行冗长的多行 Hive 查询?如下查询:
val sqlContext = new HiveContext (sc)
val result = sqlContext.sql ("
select ...
from ...
");
【问题讨论】:
-
请完善你的帖子,没有人愿意看到代码截图
标签: scala apache-spark
如何在 Spark SQL 中执行冗长的多行 Hive 查询?如下查询:
val sqlContext = new HiveContext (sc)
val result = sqlContext.sql ("
select ...
from ...
");
【问题讨论】:
标签: scala apache-spark
改用“”,例如
val results = sqlContext.sql ("""
select ....
from ....
""");
或者,如果您想格式化代码,请使用:
val results = sqlContext.sql ("""
|select ....
|from ....
""".stripMargin);
【讨论】:
stripMargin 需要管道,例如见oreilly multiline strings
您可以在 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 \
")
【讨论】:
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 """
【讨论】:
值得注意的是,长度不是问题,只是写作。为此,您可以按照 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())
【讨论】:
除了以上方式,你还可以使用下面的方式:
val results = sqlContext.sql("select .... " +
" from .... " +
" where .... " +
" group by ....
");
【讨论】: