View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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; // not declared within while loop
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