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