在scala中使用sqlContext.sql进行子查询

时间:2016-01-06 16:59:08

标签: mysql scala

我创建了以下HIVE代码,并要求将其翻译为在scala中使用。根据我的理解,我们需要使用 sqlContext.sql 在线提供的示例只有简单的选择语句。就像下面的例子一样。

例如,在scala中运行一个简单的SQL查询:

val tableA = sqlContext.sql("Select * from game");

我似乎无法对下面的代码使用相同的语法。翻译下面的代码以符合上述用法的语法是什么。

 DROP TABLE ADW.TERA_BARCODE_LOOKUP_TABLE_RAW ;

   CREATE TABLE ADW.TERA_BARCODE_LOOKUP_TABLE_RAW AS
   SELECT CAST(BRCDE_REF_I  AS STRING) AS BARCODE,
   MAX(TRIM(GST_SRC_ID)) AS GST_SRC_ID,MAX(SRC_ACTV_TS) AS SRC_ACTV_TS
   FROM

(SELECT RANKED.*
FROM
(SELECT BRCDE_REF_I,GST_SRC_ID,SRC_ACTV_TS,
RANK() over (partition by BRCDE_REF_I ORDER BY SRC_ACTV_TS DESC) AS RANK
FROM
ADW.GST_SRC_ID_BRCDE_LKUP_TABLE   X
WHERE   UPPER(X.CURR_ACTV_F) = 'Y' AND TRIM(X.GST_SRC_ID) IN
(SELECT TRIM(GST_SRC_I) FROM ADW.CANDIDATE_GST_ID_SRC_TABLE GROUP BY TRIM(GST_SRC_I))
) RANKED
WHERE RANKED.RANK = 1  ) X
GROUP BY BRCDE_REF_I ;

1 个答案:

答案 0 :(得分:2)

有两个SQL命令,因为Apache Hive doesn't support BEGIN and COMMIT最适合您的选项是将它放入两个命令中。

您尚未发布错误消息。我假设您在DROP TABLE上也可能出错,因此将其更改为DROP TABLE IF EXISTS

此外,在Scala中使用多行字符串时,您必须使用"""而不是"来包装它们。

sqlContext.sql("DROP TABLE IF EXISTS ADW.TERA_BARCODE_LOOKUP_TABLE_RAW")
sqlContext.sql("""
CREATE TABLE ADW.TERA_BARCODE_LOOKUP_TABLE_RAW AS
SELECT CAST(BRCDE_REF_I  AS STRING) AS BARCODE,
MAX(TRIM(GST_SRC_ID)) AS GST_SRC_ID,MAX(SRC_ACTV_TS) AS SRC_ACTV_TS
FROM
(SELECT RANKED.*
FROM
(SELECT BRCDE_REF_I,GST_SRC_ID,SRC_ACTV_TS,
RANK() over (partition by BRCDE_REF_I ORDER BY SRC_ACTV_TS DESC) AS RANK
FROM
ADW.GST_SRC_ID_BRCDE_LKUP_TABLE   X
WHERE   UPPER(X.CURR_ACTV_F) = 'Y' AND TRIM(X.GST_SRC_ID) IN
(SELECT TRIM(GST_SRC_I) FROM ADW.CANDIDATE_GST_ID_SRC_TABLE GROUP BY TRIM(GST_SRC_I))
) RANKED
WHERE RANKED.RANK = 1  ) X
GROUP BY BRCDE_REF_I
""")