squ*_*d21 3 postgresql scala intellij-idea apache-spark
我正在运行 scala 版本 2.12.1。使用 IntelliJ,如何使用 Spark 连接到本地 postgresql 数据库并运行 sql 命令来操作表?我有很多关于版本冲突的问题,那么是否也可以包含依赖项?
我建议你使用最新的spark,即2.2.0。对于您想做的事情,您需要spark-core、spark-sql 和postgresql jdbc 驱动程序依赖项。
对于火花使用这两个:
https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11/2.2.0 https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.11/2.2.0
对于 postgresql 驱动程序,这个驱动程序可能会做得很好:
https://mvnrepository.com/artifact/org.postgresql/postgresql/9.4.1212
Spark可以通过jdbc连接到关系数据库,spark文档中有一个关于此的部分:https ://spark.apache.org/docs/latest/sql-programming-guide.html#jdbc-to-other-databases
来自同一文档:
// Loading data from a JDBC source
val jdbcDF = spark.read
.format("jdbc")
.option("url", "jdbc:postgresql://host/database")
.option("dbtable", "schema.tablename")
.option("user", "username")
.option("password", "password")
.load()
Run Code Online (Sandbox Code Playgroud)
显然,您需要使用指定数据库的 url,对于 postgresql 连接 url,请参阅https://jdbc.postgresql.org/documentation/80/connect.html
| 归档时间: |
|
| 查看次数: |
6523 次 |
| 最近记录: |