电商项目实战-省份浏览量统计之日志分析(五)
1、LogParser.java
src/main/java/project/utils/LogParser.java
工具类
package project.utils; import org.apache.commons.lang.StringUtils; import java.util.HashMap; import java.util.Map; //日志解析 public class LogParser { IPParser ipParser = IPParser.getInstance(); public Map<String, String> parser(String log){ Map<String, String> info = new HashMap<>(); //IP是第13个字段 if (StringUtils.isNotBlank(log)){ String[] splits = log.split("\001"); String ip = splits[13]; String country = "-"; String province = "-"; String city = "-"; IPParser.RegionInfo regionInfo = ipParser.analyseIp(ip); if (regionInfo != null){ country = regionInfo.getCountry(); province = regionInfo.getProvince(); city = regionInfo.getCity(); } info.put("ip",ip); info.put("country",country); info.put("province",province); info.put("city",city); } return info; } }
2、LogParserTest.java
src/test/java/mr/project/utils/LogParserTest.java
package mr.project.utils; import org.junit.After; import org.junit.Before; import org.junit.Test; import project.utils.LogParser; import java.util.Map; public class LogParserTest { LogParser logParser; @Before public void setUp(){ logParser = new LogParser(); } @After public void tearDown(){ logParser = null; } @Test public void test01(){ Map<String, String> map = logParser.parser("20939403234\u0001http://www.yihaodian.com/1/?type=3&tracker_u=10271922392\u0001\u0001\u00013\u0001YU7317CWPWJPCCKSJD83RZ35W6F1DGHT4P93\u0001\u0001\u0001\u0001\u0001PPGDPBNYMTMMVQQSX13BVW4A6Z61BCFK\u000110271922392\u0001\\N\u0001115.28.44.205\u0001\u0001,unionKey:10271922392\u0001\u00012013-07-21 01:26:07\u0001\\N\u0001\u00013\u0001\u0001\\N\u0001null\u0001-20\u0001\u0001\u0001\u0001\u0001Mozilla/4.0 (compatible; MSIE 8.0; YYGameAll_1.2.167057.92; Windows NT 5.1; Trident/4.0; QQDownload 718; AskTbSPC2/5.13.1.18107; .NET4.0C; .NET4.0E)\u0001Win32\u0001\u0001\u0001\u0001\u0001\u0001北京市\u00012\u0001\u0001\u0001北京市\u0001\u0001\u0001\u0001\u0001\u0001\u0001\\N\u0001\\N\u0001\\N\u0001\\N\u00012013-07-21\n"); for (Map.Entry<String, String> entry : map.entrySet()){ System.out.println(entry.getKey() + " : " + entry.getValue()); } } }