Scala工具类

加载数据到mysql

package  utils

import java.io.ByteArrayInputStream
import java.lang.Class._
import java.sql.{Connection, DriverManager, ResultSet}

import com.mysql.jdbc.PreparedStatement
 

import scala.concurrent.Future

class MysqlClient(driver: String,
                  url: String,
                  username: String,
                  password: String) extends LogSupport with Serializable {
  // mysql connection
  def getConnection: Connection = {
    forName(driver)
    DriverManager.getConnection(url, username, password)
  }

  /**
    * load file to mysql.
    *
    * @param sb        the data
    * @param tableName the table name
    * @param structure the table feild
    */
  def loadFile(sb: String,
               tableName: String,
               structure: String): Unit = {
    val con = getConnection
    con.setAutoCommit(false)

    val sql = s"LOAD DATA LOCAL INFILE '' into TABLE $tableName  " +
      s"FIELDS TERMINATED BY '\t' ($structure)"
    val pstmt = con.prepareStatement(sql)
    val is = new ByteArrayInputStream(sb.getBytes())
    pstmt.unwrap(classOf[PreparedStatement]).setLocalInfileInputStream(is)

    pstmt.execute
    con.commit()

    pstmt.close()
    con.close()
  }

  /**
    * load file to mysql.
    *
    * @param sb        the data
    * @param tableName the table name
    */
  def loadFile(sb: String,
               tableName: String): Unit = {
    val con = getConnection
    con.setAutoCommit(false)

    val pstmt = con.prepareStatement(
      s"LOAD DATA LOCAL INFILE '' into TABLE $tableName  " +
        s"FIELDS TERMINATED BY '\t'")

    val is = new ByteArrayInputStream(sb.getBytes())

    pstmt.unwrap(classOf[PreparedStatement]).setLocalInfileInputStream(is)

    pstmt.execute
    con.commit()

    pstmt.close()
    con.close()
  }

  import scala.concurrent.ExecutionContext.Implicits._

  /**
    * 异步插入mysql,需注意表名
    *
    * @param data      the data
    * @param table     the table name
    * @param structure the table feild
    */
  def syncLoad(data: String,
               table: String,
               structure: String): Unit = {
    // 异步写入mysql
    val future: Future[Unit] = Future {
      log.info(s"======> load data to $table")
      if (structure == "") loadFile(data, table)
      else loadFile(data, table, structure)
    }
    future onSuccess {
      case _ => log.info("Mysql operation succuess.")
    }
    future onFailure {
      case f => log.error(s"Mysql operation error. Exception ${f.toString}")
    }
  }

  /**
    * 异步批量根性mysql
    *
    * @param sql the sql string
    */
  def syncUpdate(sql: String): Unit = {
    // 异步update mysql
    val future: Future[Unit] = Future {
      val con = getConnection
      con.prepareStatement(sql).execute()
      con.close()
    }
    future onSuccess {
      case _ => log.info("Mysql update operation succuess.")
    }
    future onFailure {
      case f => log.error(s"Mysql update operation error. Exception ${f.toString}")
    }
  }

  /**
    * 批量根性mysql
    *
    * @param sql the sql string
    */
  def update(sql: String): Unit = {
    // update mysql
    try {
      val con = getConnection
      con.prepareStatement(sql).execute()
      con.close()
    } catch {
      case e: Exception =>
        log.error(s"更新失败: sql:$sql", e)
    }
  }

  /**
    * while return.
    *
    * @tparam T
    * @return
    */
  private def results[T](resultSet: ResultSet)(f: ResultSet => T)

  = {
    new Iterator[T] {
      def hasNext: Boolean = resultSet.next()

      def next() = f(resultSet)
    }
  }

  /**
    * 批量查询mysql
    *
    * @param sql the sql string
    */
  def select[T](sql: String, f: ResultSet => (String, T)): Map[String, T] = {
    // select mysql
    try {
      val con = getConnection
      val rs = con.prepareStatement(sql).executeQuery()
      val result = results(rs)(f).toMap
      rs.close()
      con.close()
      result
    } catch {
      case e: Exception =>
        log.error(s"更新失败:sql:$sql", e)
        Map[String, T]()
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值