2

I use Spark 1.3.0 Let's say I have a dataframe in Spark and I need to store this to Postgres DB (postgresql-9.2.18-1-linux-x64) on a 64bit ubuntu machine. I also use postgresql9.2jdbc41.jar as a driver to connect to postgres

I was able to read data from postgres DB using the below commands

import org.postgresql.Driver
val url="jdbc:postgresql://localhost/postgres?user=user&password=pwd"
val driver = "org.postgresql.Driver"

val users = {
  sqlContext.load("jdbc", Map(
    "url" -> url,
    "driver" -> driver,
    "dbtable" -> "cdimemployee",
    "partitionColumn" -> "intempdimkey",
    "lowerBound" -> "0",
    "upperBound" -> "500",
    "numPartitions" -> "50"
  ))
}

val get_all_emp = users.select("*")
val empDF = get_all_emp.toDF
get_all_emp.foreach(println)

I want to write this DF back to postgres after some processing. Is this below code right?

empDF.write.jdbc("jdbc:postgresql://localhost/postgres", "test", Map("user" -> "user", "password" -> "pwd"))

Any pointers(scala) would be helpful.

4
  • Have you at least tested your code to ask if is it right or not ? Commented Sep 1, 2016 at 18:39
  • Hi @eliasah The error I'm getting is below error: value write is not a member of org.apache.spark.sql.DataFrame Commented Sep 2, 2016 at 5:33
  • Is spark 1.3 a constraint ? Can't you update to 1.6.2 ? Commented Sep 2, 2016 at 5:36
  • 1
    @eliasah That is a constraint for now Commented Sep 2, 2016 at 8:47

1 Answer 1

3

You should follow the code below.

val database = jobConfig.getString("database")
val url: String = s"jdbc:postgresql://localhost/$database"
val tableName: String = jobConfig.getString("tableName")
val user: String = jobConfig.getString("user")
val password: String = jobConfig.getString("password")
val sql = jobConfig.getString("sql")
val df = sc.sql(sql)
val properties = new Properties()
properties.setProperty("user", user)
properties.setProperty("password", password)
properties.put("driver", "org.postgresql.Driver")
df.write.mode(SaveMode.Overwrite).jdbc(url, tableName, properties)
Sign up to request clarification or add additional context in comments.

5 Comments

Maybe this is not the best explained answer ever but at least for the one who has down-voted, some comments on why it's been down voted is also good.
@VishnuJayanand I didn't say that you down-voted it. I'm just asking the one who did to give a reason.
@eliasah Sorry for no explanation but this code is what I did before I don't remember correctly but I sure that using Map instead of Properties doesn't work with Scala. So that why I give the code what I already did and succeed.
I am getting this error when I execute the first line of code error: not found: value jobConfig @giaosudau
@VishnuJayanand jobConfig just a instance of Config you can replace it by variable you use for db, password, user ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.