Spark- 求最受欢迎的TopN课程

 

数据库操作工具类

复制代码
package com.rz.mobile_tag.utils

import java.sql.{Connection, DriverManager, PreparedStatement}

object MySQLUtils {
  /**
    * 获取数据库连接
    * @return
    */
  def getConnection()={
    DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata?user=root&password=root")
  }

  /**
    * 释放数据库连接等资源
    * @param conn
    * @param pstmt
    */
  def release(conn:Connection, pstmt:PreparedStatement)={
    try {
      if (pstmt !=null){
        pstmt.close()
      }
    }catch {
      case e:Exception => e.printStackTrace()
    }finally {
      if (conn != null){
        conn.close()
      }
    }
  }
}
复制代码

数据操作类:优化点(使用批量插入数据库,提交使用batch操作)

复制代码
package com.rz.mobile_tag.dao

import java.sql.{Connection, PreparedStatement}

import com.rz.mobile_tag.bean.DayVideoAccessStat
import com.rz.mobile_tag.utils.MySQLUtils

import scala.collection.mutable.ListBuffer

object StatDao {
  /**
    * 批量保存DayVideoAccessStat到数据库
    * @param list
    */
  def insertDayVideoAccessTopN(list: ListBuffer[DayVideoAccessStat]): Unit = {
    var connection:Connection = null;
    var pstmt:PreparedStatement = null;

    try {
      connection = MySQLUtils.getConnection()

      connection.setAutoCommit(false) // 设置手动提交

      val sql ="insert into day_video_access_topn_stat(day,cms_id,times) value(?,?,?)"
      pstmt = connection.prepareStatement(sql)

      for (ele <- list){
        pstmt.setString(1, ele.day)
        pstmt.setLong(2,ele.cmsId)
        pstmt.setLong(3, ele.times)
        pstmt.addBatch()
      }
      pstmt.executeBatch() // 执行批量处理
      connection.commit() // 手工提交
    }catch {
      case e:Exception =>e.printStackTrace()
    }finally {
      MySQLUtils.release(connection, pstmt)
    }
  }
}
复制代码

 

业务实现类

复制代码
package com.rz.mobile_tag.log

import com.rz.mobile_tag.bean.DayVideoAccessStat
import com.rz.mobile_tag.dao.StatDao
import org.apache..sql.{DataFrame, Dataset, Row, SparkSession}
import org.apache..sql.functions._

import scala.collection.mutable.ListBuffer

object TopNStatJob {



  def main(args: Array[String]): Unit = {
    val  = SparkSession.builder().appName(s"${this.getClass.getSimpleName}")
      .config(".sql.sources.partitionColumnTypeInference.enabled","false")
      .master("local[2]")
      .getOrCreate()


    val accessDF: DataFrame = .read.format("parquet").load(args(0))
    accessDF.printSchema()
    accessDF.show(false)

    // 最受欢迎的TopN课程
    videoAccessTopNStat(, accessDF)

    .stop()
  }

  /**
    * 最受欢迎的TopN课程
    * @param 
    * @param accessDF
    */
  def videoAccessTopNStat(: SparkSession, accessDF: DataFrame) = {
//    import .implicits._
//    val videoAccesssTopNDF: Dataset[Row] = accessDF.filter($"day" === "20190506" && $"cmsType" === "video")
//      .groupBy("day", "cmsId")
//      .agg(count("cmsId")).as("times").orderBy($"times".desc)
//    videoAccesssTopNDF.show(false)

    accessDF.createOrReplaceTempView("access_logs")

    // 使用SQL方式进行统计
    val videoAccesssTopNDF: DataFrame = .sql("select day, cmsId, count(1) as times from access_logs" +
      " where day = '20190506' and cmsType = 'video' group by day, cmsId" +
      " order by times desc")

    //videoAccesssTopNDF.show(false)

    // 将统计数据写入到MySQL中
    try{
      videoAccesssTopNDF.foreachPartition(partitionOfRecords=>{
        val list = new ListBuffer[DayVideoAccessStat]
        partitionOfRecords.foreach(info =>{
          val day = info.getAs[String]("day")
          val cmsId = info.getAs[Long]("cmsId")
          val times = info.getAs[Long]("times")

          list.append(DayVideoAccessStat(day, cmsId, times))
        })
        StatDao.insertDayVideoAccessTopN(list)
      })
    }catch {
      case e:Exception => e.printStackTrace()
    }
  }
}
复制代码

 

 

 

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
博客
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论