//提取有用信息,转换格式 object SparkStatFormatJob { def main(args: Array[String]) = { val spark = SparkSession.builder().appName("SparkStatFormatJob").master("local[2]").getOrCreate() val access = spark.sparkContext.textFile("/Users/kingheyleung/Downloads/data/10000_access.log") //access.take(10).foreach(println) access.map(line => { val splits = line.split(" ") val ip = splits(0) //用断点的方法,观察splits数组,找出时间、url、流量对应哪个字段 //建立时间类DateUtils,转换成经常使用的时间表达方式 //把url多余的""引号清除掉 val time = splits(3) + " " + splits(4) val url = splits(11).replaceAll("\"", "") val traffic = splits(9) //(ip, DateUtils.parse(time), url, traffic) 用来测试输出是否正常 //把裁剪好的数据从新组合,用Tab分割 DateUtils.parse(time) + "\t" + url + "\t" + traffic + "\t" + ip }).saveAsTextFile("file:///usr/local/mycode/immooclog/") spark.stop() } }
//日期解析 object DateUtils { //输入格式 val ORIGINAL_TIME_FORMAT = FastDateFormat.getInstance("dd/MMM/yyyy:HH:mm:sss Z", Locale.ENGLISH) //输出格式 val TARGET_TIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss") def parse(time:String) = { TARGET_TIME_FORMAT.format(new Date(getTime(time))) } def getTime(time:String) = { try { ORIGINAL_TIME_FORMAT.parse(time.substring(time.indexOf("[") + 1, time.lastIndexOf("]"))).getTime } catch { case e : Exception => { 0l } } }
通常日志处理须要进行分区java
//解析日志 object SparkStatCleanJob { def main(args: Array[String]) = { val spark = SparkSession.builder().appName("SparkStatCleanJob").master("local[2]").getOrCreate() val accessRDD = spark.sparkContext.textFile("file:///Users/kingheyleung/Downloads/data/access_10000.log") //RDD convert to DF, define Row and StructType val accessDF = spark.createDataFrame(accessRDD.map(line => LogConvertUtils.convertToRow(line)), LogConvertUtils.struct) //accessDF.printSchema() //accessDF.show(false) spark.stop() } }
//RDD转换成DF的工具类 object LogConvertUtils { //构建Struct val struct = StructType( Array( StructField("url", StringType), StructField("cmsType", StringType), StructField("cmsId", LongType), StructField("traffic", LongType), StructField("ip", StringType), StructField("city", StringType), StructField("time", StringType), StructField("day", StringType) ) ) //提取信息,构建Row def convertToRow(line:String) = { try { val splits = line.split("\t") val url = splits(1) val traffic = splits(2).toLong val ip = splits(3) val domain = "http://www.imooc.com/" val cms = url.substring(url.indexOf(domain) + domain.length()) val cmsSplits = cms.split("/") var cmsType = "" var cmsId = 0l //判断是否存在 if (cmsSplits.length > 1) { cmsType = cmsSplits(0) cmsId = cmsSplits(1).toLong } val city = IpUtils.getCity(ip) //经过Ip解析工具传进,具体看下面 val time = splits(0) val day = time.substring(0, 10).replaceAll("-", "") //定义Row,与Struct同样 Row(url, cmsType, cmsId, traffic, ip, city, time, day) } catch { case e: Exception => Row(0) } } }
注意:转换时必定要记得类型转换!!!!mysql
//完成统计操做 object TopNStatJob { def main(args: Array[String]) { val spark = SparkSession.builder().appName("TopNStatJob") .config("spark.sql.sources.partitionColumnTypeInference.enabled", "false") .master("local[2]").getOrCreate() val accessDF = spark.read.format("parquet").load("/Users/kingheyleung/Downloads/data/clean/") dfCountTopNVideo(spark, accessDF) sqlCountTopNVideo(spark, accessDF) //accessDF.printSchema() spark.stop() } def dfCountTopNVideo(spark: SparkSession, accessDF: DataFrame): Unit = { /* * DF API * */ //导入隐式转换, 留意$号的使用, 而且导入functions包,使agg聚合函数count可以使用,此处若不用$的话,就没法让times进行desc排序了 import spark.implicits._ val topNDF = accessDF.filter($"day" === "20170511" && $"cmsType" === "video") .groupBy("day", "cmsId").agg(count("cmsId").as("times")).orderBy($"times".desc) topNDF.show(false) } def sqlCountTopNVideo(spark: SparkSession, accessDF: DataFrame): Unit = { /* * SQL API * */ //建立临时表access_view,注意换行时,很容易忽略掉空格 accessDF.createTempView("access_view") val topNDF = spark.sql("select day, cmsId, count(1) as times from access_view " + "where day == '20170511' and cmsType == 'video' " + "group by day, cmsId " + "order by times desc") topNDF.show(false) } }
/* * 链接MySQL数据库 * 操做工具类 * */ object MySQLUtils { //得到链接 def getConnection(): Unit = { DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc_project?user=root&password=666") } //释放资源 def release(connection: Connection, pstmt: PreparedStatement): Unit = { try { if (pstmt != null) { pstmt.close() } } catch { case e: Exception => e.printStackTrace() } finally { connection.close() } } }
//课程访问次数实体类 case class VideoAccessStat(day: String, cmsId:Long, times: Long) /* * 各个维度统计的DAO操做 * */ object StatDAO { /* * 批量保存VideoAccessStat到数据库 * */ def insertDayAccessTopN(list: ListBuffer[VideoAccessStat]): Unit = { var connection: Connection = null //jdbc的准备工做, 定义链接 var pstmt: PreparedStatement = null try { connection = MySQLUtils.getConnection() //真正获取链接 connection.setAutoCommit(false) //为了实现批处理,要关掉默认的自动提交 val sql = "insert into day_topn_video(day, cms_id, times) values (?, ?, ?)" //占位符 pstmt = connection.prepareStatement(sql) //把SQL语句生成pstmt对象,后面才能够填充占位符中的数据 for (ele <- list) { pstmt.setString(1, ele.day) pstmt.setLong(2, ele.cmsId) pstmt.setLong(3, ele.times) pstmt.addBatch() //加入批处理 } pstmt.execute() //执行批量处理 connection.commit() //手工提交 } catch { case e: Exception => e.printStackTrace() } finally { MySQLUtils.release(connection, pstmt) } } }
try { topNDF.foreachPartition(partitionOfRecords => { // val list = new ListBuffer[VideoAccessStat] //建立list来装统计记录 //遍历每一条记录,取出来上面对应的三个字段day,cmsId,times partitionOfRecords.foreach(info => { val day = info.getAs[String]("day") //后面的就是取出来的记录的每一个字段 val cmsId = info.getAs[Long]("cmsId") val times = info.getAs[Long]("times") //每一次循环建立一个VideoAccessStat对象,添加一次进入list中 list.append(VideoAccessStat(day, cmsId, times)) }) //把list传进DAO类 StatDAO.insertDayAccessTopN(list) }) } catch { case e: Exception => e.printStackTrace() }
//先计算访问次数,并按照day,cmsId,city分组 val cityAccessTopNDF = accessDF.filter(accessDF.col("day") === "20170511" && accessDF.col("cmsType") === "video") .groupBy("day", "cmsId", "city").agg(count("cmsId").as("times")) //进行分地市排序,使用到row_number函数,生成一个排名,定义为time_rank, 而且取排名前3 cityAccessTopNDF.select( cityAccessTopNDF.col("day"), cityAccessTopNDF.col("cmsId"), cityAccessTopNDF.col("times"), cityAccessTopNDF.col("city"), row_number().over(Window.partitionBy(cityAccessTopNDF.col("city")) .orderBy(cityAccessTopNDF.col("times").desc) ).as("times_rank") ).filter("times_rank <= 3").show(false) }
def deleteDayData(day: String) = { var connection: Connection = null var pstmt: PreparedStatement = null var tables = Array("day_topn_video", "day_city_topn_video", "traffic_topn_video" ) try { connection = MySQLUtils.getConnection() for (table <- tables) { val deleteSql = s"delete from $table where day = ?” //Scala特殊处理 pstmt = connection.prepareStatement(deleteSql) pstmt.setString(1, table) pstmt.setString(2, day) pstmt.executeUpdate() } } catch { case e: Exception => e.printStackTrace() } finally { MySQLUtils.release(connection, pstmt) } }