处理本地日志记录

This commit is contained in:
2018-04-20 16:58:54 +08:00
parent eba6a66132
commit 26bd4a5c25
6 changed files with 411 additions and 86 deletions

View File

@@ -0,0 +1,74 @@
package com.fjy.hadoop;
import com.kumkee.userAgent.UserAgent;
import com.kumkee.userAgent.UserAgentParser;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* @author F嘉阳
* @date 2018-04-20 16:31
* UserAgentParser测试类
*/
public class UserAgentTest {
@Test
public void testReadFile() throws Exception {
String path = "F:\\JAVA Workspace\\hadoopstudy\\access.log";
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path))));
String line = "";
int count = 0;
UserAgentParser userAgentParser = new UserAgentParser();
while (line != null) {
line = reader.readLine();
count++;
if (StringUtils.isNotBlank(line)) {
String source = line;
UserAgent agent = userAgentParser.parse(source);
//测试
String browser = agent.getBrowser();
String engine = agent.getEngine();
String engineVersion = agent.getEngineVersion();
String os = agent.getOs();
String platform = agent.getPlatform();
boolean mobile = agent.isMobile();
//输出解析的信息
System.out.println(browser + "," + engine + "," + engineVersion + "," + os + "," + platform + "," + mobile);
}
}
System.out.println("总记录数:"+count);
}
/**
* UserAgent工具类的单元测试
*/
@Test
public void UserAgentTestParser() {
//String source = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";
String source = "Mozilla/5.0 (Linux; Android 8.0.0; ONEPLUS A5010 Build/OPR6.170623.013) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36";
UserAgentParser userAgentParser = new UserAgentParser();
UserAgent agent = userAgentParser.parse(source);
//测试
String browser = agent.getBrowser();
String engine = agent.getEngine();
String engineVersion = agent.getEngineVersion();
String os = agent.getOs();
String platform = agent.getPlatform();
boolean mobile = agent.isMobile();
//输出解析的信息
System.out.println(browser + "," + engine + "," + engineVersion + "," + os + "," + platform + "," + mobile);
}
}