代码之家  ›  专栏  ›  技术社区  ›  tree em

使用scala spark将参数传递到sql字符串三元组

  •  -1
  • tree em  · 技术社区  · 5 年前
    val BusinessDate = '2019-02-06'
    val query = """(SELECT *
                      |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= '2019-02-06' AND a.Balance IS NOT NULL AND
                      | isnull(a.Classification,'N/A') IN ('Contractual Account','Non-Term Deposit','Term Deposit')) tmp """.stripMargin
    
    val responseWithSelectedColumns = spark
          .read
          .format("jdbc")
          .option("url", url)
          .option("driver", driver)
          .option("dbtable", query)
          .load()
    
        print("TOTAL: "+responseWithSelectedColumns.count())
    

    c.Businessdate= '2019-02-06' , 我希望将BusinessDate作为参数传递,而不是像这样直接传递。

    我该怎么做?

    1 回复  |  直到 5 年前
        1
  •  1
  •   shay__    5 年前

    这叫做 string interpolation

    val BusinessDate = '2019-02-06'
    val query = s"""(SELECT *
                      |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= '$BusinessDate' AND a.Balance IS NOT NULL AND
                      | isnull(a.Classification,'N/A') IN ('Contractual Account','Non-Term Deposit','Term Deposit')) tmp """.stripMargin