-
Notifications
You must be signed in to change notification settings - Fork 0
/
Purify.java
58 lines (46 loc) · 1.81 KB
/
Purify.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package offline;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import db.mongodb.MongoDBUtil;
public class Purify {
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase(MongoDBUtil.DB_NAME);
String fileName = "..."; // your tomcat_log file
try {
db.getCollection("logs").drop();
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
// Sample input:
// 73.223.210.212 - - [19/Aug/2017:22:00:24 +0000] "GET /EventRecommender/history?user_id=1111 HTTP/1.1" 200 11410
List<String> values = Arrays.asList(line.split(" "));
String ip = values.size() > 0 ? values.get(0) : null;
String timestamp = values.size() > 3 ? values.get(3) : null;
String method = values.size() > 5 ? values.get(5) : null;
String url = values.size() > 6 ? values.get(6) : null;
String status = values.size() > 8 ? values.get(8) : null;
Pattern pattern = Pattern.compile("\\[(.+?):(.+)");
Matcher matcher = pattern.matcher(timestamp);
matcher.find();
db.getCollection("logs")
.insertOne(new Document().append("ip", ip).append("date", matcher.group(1))
.append("time", matcher.group(2)).append("method", method.substring(1))
.append("url", url).append("status", status));
}
System.out.println("Import Done!");
bufferedReader.close();
mongoClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}