Spark-项目中分析日志的核心代码

Posted 07H_JH

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spark-项目中分析日志的核心代码相关的知识,希望对你有一定的参考价值。

代码

LogRecord 类:

case class LogRecord (
    clientIpAddress: String,      
    rfc1413ClientIdentity: String,   
    remoteUser: String,             `
    dateTime: String,              //[day/month/year:hour:minute:second zone]
    request: String,                 
    httpStatusCode: String,          
    bytesSent: String,              
    referer: String,                
    userAgent: String                
)

LogParser 解析类

import java.util.regex.Pattern
import java.text.SimpleDateFormat
import java.util.Locale
import scala.util.control.Exception._
import java.util.regex.Matcher
import scala.util.{Try, Success, Failure}

@SerialVersionUID(100L)
class LogParser extends Serializable {

    private val ddd = "\\d{1,3}"                      
    private val ip = s"($ddd\\.$ddd\\.$ddd\\.$ddd)?"  
    private val client = "(\\S+)"                     
    private val user = "(\\S+)"
    private val dateTime = "(\\[.+?\\])"              
    private val request = "\"(.*?)\""                 
    private val status = "(\\d{3})"
    private val bytes = "(\\S+)"                      
    private val referer = "\"(.*?)\""
    private val agent = "\"(.*?)\""
    private val regex = s"$ip $client $user $dateTime $request $status $bytes $referer $agent"
    private val p = Pattern.compile(regex)


    def parseRecord(record: String): Option[AccessLogRecord] = {
        val matcher = p.matcher(record)
        if (matcher.find) {
            Some(buildAccessLogRecord(matcher))
        } else {
            None
        }
    }

    def parseRecordReturningNullObjectOnFailure(record: String): AccessLogRecord = {
        val matcher = p.matcher(record)
        if (matcher.find) {
            buildAccessLogRecord(matcher)
        } else {
            AccessLogParser.nullObjectAccessLogRecord
        }
    }

    private def buildAccessLogRecord(matcher: Matcher) = {
        AccessLogRecord(
            matcher.group(1),
            matcher.group(2),
            matcher.group(3),
            matcher.group(4),
            matcher.group(5),
            matcher.group(6),
            matcher.group(7),
            matcher.group(8),
            matcher.group(9))
    }
}

/**
 * 例子:
 * 94.102.63.11 - - [21/Jul/2009:02:48:13 -0700] "GET / HTTP/1.1" 200 18209 "http://acme.com/foo.php" "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
 */
object AccessLogParser {

    val nullObjectAccessLogRecord = AccessLogRecord("", "", "", "", "", "", "", "", "")

    def parseRequestField(request: String): Option[Tuple3[String, String, String]] = {
        val arr = request.split(" ")
        if (arr.size == 3) Some((arr(0), arr(1), arr(2))) else None
    }
    def parseDateField(field: String): Option[java.util.Date] = {
        val dateRegex = "\\[(.*?) .+]"
        val datePattern = Pattern.compile(dateRegex)
        val dateMatcher = datePattern.matcher(field)
        if (dateMatcher.find) {
                val dateString = dateMatcher.group(1)
                println("***** DATE STRING" + dateString)
                // HH is 0-23; kk is 1-24
                val dateFormat = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH)
                allCatch.opt(dateFormat.parse(dateString))  // return Option[Date]
            } else {
            None
        }
    }

}

总结

日志分析是经常做的事情,大数据下的日志分析也是一个常用技术。

以上是关于Spark-项目中分析日志的核心代码的主要内容,如果未能解决你的问题,请参考以下文章

在 Linux 中分析 Node.JS(代码)执行时间

如何在 Ruby 中分析 `require` 语句?

如何从核心转储中分析内存使用情况?

不要在 Xcode 中分析 pod

从源码和日志文件结构中分析Kafka重启失败事件

如何在 R 中分析和优化已经快速的代码