Chinaunix首页 | 论坛 | 博客
  • 博客访问: 374847
  • 博文数量: 85
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 657
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-17 20:48
个人简介

行到水穷处,坐看云起时

文章分类

全部博文(85)

文章存档

2019年(2)

2018年(1)

2016年(1)

2015年(66)

2014年(15)

我的朋友

分类: 大数据

2015-03-23 09:11:26

  def updateTableRows(tableName : String, rdd : RDD[_]){  
      rdd.foreachPartition(p => {
        var conn:Connection = null
        
        try{     
          conn = getConnection()
          val stmt = conn.createStatement()
        
          p.foreach(x => {
            
            var sql = ""
            x match {
              case ((p1:Int,p2:String),p3:Int) => sql = "insert into " + tableName + " " + tblToCols(tableName)(1) + " values " + "(" + p1 + ",'" + escapeChar(p2) + "'," + p3 + ");"
              case ((p1:Int,p2:Int,p3:Int,p4:Int),p5:Int) => sql = "insert into " + tableName + " " + tblToCols(tableName)(1) + " values " + "(" + p1 + "," + p2 + "," + p3 + "," + p4 + "," + p5 + ");"
            }
            
            stmt.execute(sql); 
          }) 
        }catch{
          case e:Exception => e.printStackTrace()
          sys.exit(1)
        }finally{
          if(conn != null){
             conn.close()
          }
        }
      })
  }
  
  
  def  getConnection() : Connection = {
    var conn : Connection = null
    
    try{
       Class.forName(driver)
       conn = DriverManager.getConnection(url, user, password)
    }catch{
       case e:Exception => e.printStackTrace()
    }
    
    conn
  }
阅读(978) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~