1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.database;
19
20 import java.io.*;
21 import java.sql.*;
22 import java.util.*;
23 import org.apache.hadoop.chukwa.util.ExceptionUtil;
24 import org.apache.hadoop.chukwa.util.DatabaseWriter;
25 import org.apache.hadoop.chukwa.database.TableCreator;
26
27 public class DatabaseSetup {
28 public long[] timeWindow = {7, 30, 91, 365, 3650};
29 public String[] tables = {"system_metrics","disk","cluster_system_metrics","cluster_disk","mr_job","mr_task","dfs_namenode","dfs_datanode","dfs_fsnamesystem","dfs_throughput","hadoop_jvm","hadoop_mapred","hdfs_usage"};
30 public String cluster = "demo";
31 public long current = Calendar.getInstance().getTimeInMillis();
32
33 public void setUpDatabase() throws Exception {
34 System.setProperty("CLUSTER","demo");
35 DatabaseWriter db = new DatabaseWriter(cluster);
36 String buffer = "";
37 File aFile = new File(System.getenv("CHUKWA_CONF_DIR")
38 + File.separator + "database_create_tables.sql");
39 buffer = readFile(aFile);
40 String tables[] = buffer.split(";");
41 for(String table : tables) {
42 if(table.length()>5) {
43 db.execute(table);
44 }
45 }
46 db.close();
47 for(int i=0;i<timeWindow.length;i++) {
48 TableCreator tc = new TableCreator();
49 long start = current;
50 long end = current + (timeWindow[i]*1440*60*1000);
51 tc.createTables(start, end);
52 }
53 }
54
55 public void tearDownDatabase() {
56 DatabaseWriter db = null;
57 try {
58 db = new DatabaseWriter(cluster);
59 ResultSet rs = db.query("show tables");
60 ArrayList<String> list = new ArrayList<String>();
61 while(rs.next()) {
62 String table = rs.getString(1);
63 list.add(table);
64 }
65 for(String table : list) {
66 db.execute("drop table "+table);
67 }
68 } catch(Throwable ex) {
69 } finally {
70 if(db!=null) {
71 db.close();
72 }
73 }
74 }
75
76 public String readFile(File aFile) {
77 StringBuffer contents = new StringBuffer();
78 try {
79 BufferedReader input = new BufferedReader(new FileReader(aFile));
80 try {
81 String line = null;
82 while ((line = input.readLine()) != null) {
83 contents.append(line);
84 contents.append(System.getProperty("line.separator"));
85 }
86 } finally {
87 input.close();
88 }
89 } catch (IOException ex) {
90 ex.printStackTrace();
91 }
92 return contents.toString();
93 }
94
95 }
96