2017-07-18 46 views

回答

1

我建议你使用最新的火花,即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

星火可以连接到通过JDBC的关系型数据库,有在这个节火花文档: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:dbserver") 
    .option("dbtable", "schema.tablename") 
    .option("user", "username") 
    .option("password", "password") 
    .load() 

很明显,你将需要使用指定你的数据库的URL,PostgreSQL的连接网址看到https://jdbc.postgresql.org/documentation/80/connect.html

+0

它得到的错误:错误:(11,24)对象读不包org.apache成员.spark val jdbcDF = spark.read – squad21

+0

代码片段中的'spark'引用SparkSession对象,它有一个名为read的方法。 – oh54