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  
19  package org.apache.hadoop.chukwa.database;
20  
21  
22  import java.sql.ResultSet;
23  import java.sql.SQLException;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.chukwa.util.DatabaseWriter;
31  import org.apache.hadoop.chukwa.util.ExceptionUtil;
32  
33  public class TableCreator {
34    private static DatabaseConfig dbc = null;
35    private static Log log = LogFactory.getLog(TableCreator.class);
36  
37    public TableCreator() {
38      if (dbc == null) {
39        dbc = new DatabaseConfig();
40      }
41    }
42  
43    public void createTables() throws Exception {
44      long now = (new Date()).getTime();
45      createTables(now, now);
46    }
47  
48    public void createTables(long start, long end) throws Exception {
49      String cluster = System.getProperty("CLUSTER");
50      if (cluster == null) {
51        cluster = "unknown";
52      }
53      DatabaseWriter dbw = new DatabaseWriter(cluster);
54      HashMap<String, String> dbNames = dbc.startWith("report.db.name.");
55      Iterator<String> ki = dbNames.keySet().iterator();
56      while (ki.hasNext()) {
57        String name = ki.next();
58        String tableName = dbNames.get(name);
59        String[] tableList = dbc.findTableName(tableName, start, end);
60        log.debug("table name: " + tableList[0]);
61        try {
62          String[] parts = tableList[0].split("_");
63          int partition = Integer.parseInt(parts[parts.length - 2]);
64          String table = "";
65          for (int i = 0; i < parts.length - 2; i++) {
66            if (i != 0) {
67              table = table + "_";
68            }
69            table = table + parts[i];
70          }
71          String query = "show create table " + table + "_template;";
72          ResultSet rs = dbw.query(query);
73          while (rs.next()) {
74            log.debug("table schema: " + rs.getString(2));
75            query = rs.getString(2);
76            log.debug("template table name:" + table + "_template");
77            log.debug("replacing with table name:" + table + "_" + partition
78                + "_" + parts[parts.length - 1]);
79            log.debug("creating table: " + query);
80            String createPartition = query.replaceFirst(table + "_template",
81                table + "_" + partition + "_" + parts[parts.length - 1]);
82            createPartition = createPartition.replaceFirst("TABLE",
83                "TABLE IF NOT EXISTS");
84            dbw.execute(createPartition);
85            partition++;
86            createPartition = query.replaceFirst(table + "_template", table
87                + "_" + partition + "_" + parts[parts.length - 1]);
88            createPartition = createPartition.replaceFirst("TABLE",
89                "TABLE IF NOT EXISTS");
90            dbw.execute(createPartition);
91            partition++;
92            createPartition = query.replaceFirst(table + "_template", table
93                + "_" + partition + "_" + parts[parts.length - 1]);
94            createPartition = createPartition.replaceFirst("TABLE",
95                "TABLE IF NOT EXISTS");
96            dbw.execute(createPartition);
97          }
98        } catch (NumberFormatException e) {
99          log.error("Error in parsing table partition number, skipping table:"
100             + tableList[0]);
101       } catch (ArrayIndexOutOfBoundsException e) {
102         log.debug("Skipping table:" + tableList[0]
103             + ", because it has no partition configuration.");
104       } catch (SQLException e) {
105         throw e;
106       }
107     }
108   }
109 
110   public static void usage() {
111     System.out.println("TableCreator usage:");
112     System.out
113         .println("java -jar chukwa-core.jar org.apache.hadoop.chukwa.TableCreator <date> <time window size>");
114     System.out.println("     date format: YYYY-MM-DD");
115     System.out.println("     time window size: 7, 30, 91, 365, 3650");
116   }
117 
118   public static void main(String[] args) {
119     TableCreator tc = new TableCreator();
120     if (args.length == 2) {
121       try {
122         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
123         long start = sdf.parse(args[0]).getTime();
124         long end = start + (Long.parseLong(args[1]) * 1440 * 60 * 1000L);
125         tc.createTables(start, end);
126       } catch (Exception e) {
127         System.out.println("Invalid date format or time window size.");
128         e.printStackTrace();
129         usage();
130       }
131     } else {
132       try {
133         tc.createTables();
134       } catch (Exception e) {
135         log.error(ExceptionUtil.getStackTrace(e));
136       }
137     }
138 
139   }
140 }