Spark SQL Dataframe 写入oracle

可以看此处文章https://xvlvzhu.github.io/2018/01/07/Spark%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93%E7%A4%BA%E4%BE%8B/
以下是写入Oracle数据库

 val url = "jdbc:oracle:thin:@//ip:1521/数据库名称"
  val user = "ods"
  val password = "oracle"
  val driver = "oracle.jdbc.driver.OracleDriver"
  val dbmap = Map("url" -> url, "user" -> user, "password" -> password, "driver" -> driver)
  val properties = new Properties()
  properties.setProperty("url", url)
  properties.setProperty("user", user)
  properties.setProperty("password", password)
  properties.setProperty("driver", driver)

  def saveSummary(rowrdd: RDD[Row], sqlContext: SQLContext): Unit = {
     
    //DateTypes.createStructField()
   val schema= StructType(List(StructField("mon_th",IntegerType, true)
      , StructField("current_date", DateType, true)
      , StructField("amount", DoubleType, true)
      , StructField("account_code", StringType, true)
      , StructField("summary_code", StringType, true)
      , StructField("project", StringType, true)
    ))
    val dataFrame=sqlContext.createDataFrame(rowrdd,schema)
    dataFrame.show()
    dataFrame.write.mode(SaveMode.Append).jdbc(url,"表名",properties)
  }

你可能感兴趣的:(笔记,oracle,spark)