【问题标题】:Passing variable to Postgresql from Spark with scala使用scala将变量从Spark传递给Postgresql
【发布时间】:2020-05-04 18:50:30
【问题描述】:

通常我可以使用以下命令从 Spark 访问 Hive 表:

val start = "20191009"
val end   = "20200112"
val df= ss.sql(s"""select * from myTable where dat between '$start' and '$end' """)

通过之前的代码,我可以通过在变量前面包含$ 将变量传递给SQL

现在我想使用 postgres 表执行相同的先前逻辑。 我有 Postgres 表,我通过它连接到它:

val statement = connection.createStatement()

var gg = statement.executeQuery("update myTable set firstV='NewValue' where SecondV =$val;")

我想将一个变量传递给前一个 sql(到 val 变量)。

【问题讨论】:

    标签: postgresql scala apache-spark hive


    【解决方案1】:

    您的第二个代码 sn-p 未使用 s 修饰符,因此 $ 替换将不起作用。试试看

    var gg = statement.executeQuery(s"update myTable set firstV='NewValue' where SecondV =$val;")```
    

    【讨论】:

      【解决方案2】:

      @Charlie Flowers 解决了部分问题;我错过了查询前面的s 修饰符。另一个问题是试图将 Postgres 表的结果保存在一个变量中(在我的例子中是 gg),查询将直接在 Postgres 数据库/表上执行,所以我只是删除了 var gg = 部分。

      statement.executeQuery(s"update myTable set firstV='NewValue' where SecondV =$val;")
      

      另外,this 帮助了我很多,因为我使用了executeQuery,而我必须使用executeUpdate,所以我的查询应该是:

      statement.executeUpdate(s"update myTable set firstV='NewValue' where SecondV =$val;")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-11
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        相关资源
        最近更新 更多