使用scala从Spark向Postgresql传递变量
通常,我可以通过以下方式从Spark访问配置单元表:使用scala从Spark向Postgresql传递变量,postgresql,scala,apache-spark,hive,Postgresql,Scala,Apache Spark,Hive,通常,我可以通过以下方式从Spark访问配置单元表: val start = "20191009" val end = "20200112" val df= ss.sql(s"""select * from myTable where dat between '$start' and '$end' """) 通过前面的代码,我可以通过在变量前面包含$将变量传递给SQL 现在我想使用前面的逻辑,但是使用postgres表。 我有一个Postgres表,我通过以下方式连接到它: val sta
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(to val变量)。您的第二个代码段没有使用
s
修饰符,因此$
替换将不起作用。试一试
var gg = statement.executeQuery(s"update myTable set firstV='NewValue' where SecondV =$val;")```
您的第二个代码段未使用
s
修饰符,因此$
替换将不起作用。试一试
var gg = statement.executeQuery(s"update myTable set firstV='NewValue' where SecondV =$val;")```
正如《查理·弗劳尔斯》(Charlie Flowers)触及了问题的一部分;我错过了查询前面的
s
修饰符。另一个问题是试图将Postgres表的结果保存在一个变量中(在我的例子中是gg
),查询将直接在Postgres数据库/表上执行,所以我刚刚删除了var gg=
部分
statement.executeQuery(s"update myTable set firstV='NewValue' where SecondV =$val;")
另外,这对我帮助很大,因为我使用了executeQuery
,而我必须使用executeUpdate
,所以我的查询应该是:
statement.executeUpdate(s"update myTable set firstV='NewValue' where SecondV =$val;")
正如《查理·弗劳尔斯》(Charlie Flowers)触及了问题的一部分;我错过了查询前面的
s
修饰符。另一个问题是试图将Postgres表的结果保存在一个变量中(在我的例子中是gg
),查询将直接在Postgres数据库/表上执行,所以我刚刚删除了var gg=
部分
statement.executeQuery(s"update myTable set firstV='NewValue' where SecondV =$val;")
另外,这对我帮助很大,因为我使用了executeQuery
,而我必须使用executeUpdate
,所以我的查询应该是:
statement.executeUpdate(s"update myTable set firstV='NewValue' where SecondV =$val;")