注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
刚做完twitter OA Test 5, 遇到apache log这个问题。网上暂时没搜到代码,所以在这里把test 全过的code发一遍
代码是Scala写的,应该能简单转换为Java
//10.10.10.10 - - [27/Sep/2016:05:22:00 +0000]
// "GET /1.1/friendships/list.json?user_id=123 HTTP/1.1" 500 563 19 "Twitter-iPhone/6.63 iOS/10.0.2 (Apple;iPhone7,2;;;;;1)" 177.177.177.177
sealed case class ApcLog(ipSelf: String, str2: String, str3: String, // String time;
time: Date, method: String // 5
, rawUrl: String, uri: String, protocol: String,
respCode: Int, respSize: Long, int10: Long,
agent: String, ipClient: String) {}
object TWS extends App {
val strLogPtn =
// 1:IP 2:client 3:user 4:date time 5:method 6:req 7:proto 8:resp 9:sz 10.int 11 ", uri) =>
printf(s"${fmtOut.format(time)} $uri %.2f\r\n", rate)
}
// 2016-09-27T05:22 /1.1/friendships/list.json 75.00
// 2016-09-27T05:23 /1.1/users/show.json 100.00
// 2016-09-27T22:45 /1.1/friendships/create.json 100.00
// 2016-09-27T22:45 /1.1/friendships/list.json 100.00
}
补充内容 (2017-11-19 08:24):
如果觉得有用,求加大米!
补充内容 (2017-11-19 08:28):
简单说一下思路:用一个正则表达式把log中的字段全都取出来,int转换为int,time用formatterparse一下,然后创建一个log的object,剩下的内容就是在这堆object上建些索引,算些rate,再打印出来。 |