Spark SQL实现日志离线批处理
- 数据采集 Flume:Web日志写入到HDFS
- 数据清洗 脏数据 Spark、Hive、MR等计算框架来完成。 清洗完之后再放回HDFS
- 数据处理 按照需要,进行业务的统计和分析。 也通过计算框架完成
- 处理结果入库 存放到RDBMS、NoSQL中
- 数据可视化 通过图形化展示出来。 ECharts、HUE、Zeppelin
- 统计某时间段最受欢迎的某项的TopN和对应的访问次数
- 按地市统计最受欢迎 从IP提取城市信息
- 按访问流量统计
- 读取日志文件,得到RDD,通过map方法,split成一个数组,然后选择数组中有用的几项(用断点的方法分析哪几项有用,并匹配相应的变量)
- 获取到的信息有可能因为某些问题,如线程问题而导致生成了带有错误的信息,第一步中一开始用了SimpleDateFormat(线程不安全)来转变时间格式,会导致某些时间转换错误。一般要改成FastDateFormat来做
//提取有用信息,转换格式 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 } } }
一般日志处理需要进行分区
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31933288
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31933283
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31931284
http://www.bokee.net/bloggermodule/blog_viewblog.do?id=31931268
//解析日志 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) } } }
注意:转换时一定要记得类型转换!!!!
- 使用DataFrame API统计分析
- SQL API
//完成统计操作 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) } }
- 使用DriverManager,连接到mysql 3306
- 释放资源,connection和preparedstatement都要,注意处理异常
/* * 连接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() } } }
- 在mysql中创建一张表,包含day,cms_Id,times三个字段(注意各自的数据类型,以及定义不允许为NULL,并把day和cms_Id作为PRI KEY)
- 创建模型类case class,三个输入参数,day、cms_Id,times
- 创建操作数据库DAO类,输入的参数是一个list,list装的是上面的模型类,目的是插入insert记录到数据库中,DAO中分以下几步:
- 首先,做jdbc连接的准备,创建connection和prepareStatement,把关闭连接也写好,用try catch finally抛出异常;
- 然后写sql语句,preparestatement需要赋值的地方用占位符放着;
- 进行对list遍历,把每个对象都放进pstmt中
- 调优点!!!遍历前把自动提交关掉,遍历中把pstmt加入批处理中,遍历完后执行批处理操作!最后手工提交连接
//课程访问次数实体类 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) } } }
- 创建模型类对应的list
- 对记录进行遍历,把记录的每个字段当做参数,创建模型类对象
- 把每个对象添加到list中
- 把list传进DAO类中
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) } }