Package org.apache.hadoop.chukwa.util

Examples of org.apache.hadoop.chukwa.util.DatabaseWriter


            try {
                String cluster = System.getProperty("CLUSTER");
                if(cluster==null) {
                    cluster="unknown";
                }
                db = new DatabaseWriter(cluster);
                DatabaseMetaData dbMetaData = db.getConnection().getMetaData();
                ResultSet rs = dbMetaData.getColumns ( null,null,table[0], null);
                boolean first=true;
                while(rs.next()) {
                    if(!first) {
View Full Code Here


      Calendar now = Calendar.getInstance();
      String cluster = System.getProperty("CLUSTER");
      if (cluster == null) {
        cluster = "unknown";
      }
      DatabaseWriter db = new DatabaseWriter(cluster);
      String fields = null;
      String dateclause = null;
      boolean emptyPrimeKey = false;
      log.info("Consolidate for " + interval + " minutes interval.");

      String[] tmpTable = dbc.findTableName(this.table, start, end);
      String table = tmpTable[0];
      String sumTable = "";
      if (interval == 5) {
        long partition = now.getTime().getTime() / DatabaseConfig.WEEK;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(this.table);
        stringBuilder.append("_");
        stringBuilder.append(partition);
        stringBuilder.append("_week");
        table = stringBuilder.toString();
        long partition2 = now.getTime().getTime() / DatabaseConfig.MONTH;
        sumTable = this.table + "_" + partition2 + "_month";
      } else if (interval == 30) {
        long partition = now.getTime().getTime() / DatabaseConfig.MONTH;
        table = this.table + "_" + partition + "_month";
        long partition2 = now.getTime().getTime() / DatabaseConfig.QUARTER;
        sumTable = this.table + "_" + partition2 + "_month";
      } else if (interval == 180) {
        long partition = now.getTime().getTime() / DatabaseConfig.QUARTER;
        table = this.table + "_" + partition + "_quarter";
        long partition2 = now.getTime().getTime() / DatabaseConfig.YEAR;
        sumTable = this.table + "_" + partition2 + "_month";
      } else if (interval == 720) {
        long partition = now.getTime().getTime() / DatabaseConfig.YEAR;
        table = this.table + "_" + partition + "_year";
        long partition2 = now.getTime().getTime() / DatabaseConfig.DECADE;
        sumTable = this.table + "_" + partition2 + "_month";
      }
      // Find the most recent entry
      try {
        String query = "select * from " + sumTable
            + " order by timestamp desc limit 1";
        log.debug("Query: " + query);
        rs = db.query(query);
        if (rs == null) {
          throw new SQLException("Table is undefined.");
        }
        ResultSetMetaData rmeta = rs.getMetaData();
        boolean empty = true;
        if (rs.next()) {
          for (int i = 1; i <= rmeta.getColumnCount(); i++) {
            if (rmeta.getColumnName(i).toLowerCase().equals("timestamp")) {
              start = rs.getTimestamp(i).getTime();
            }
          }
          empty = false;
        }
        if (empty) {
          throw new SQLException("Table is empty.");
        }
        end = start + (interval * 60000);
      } catch (SQLException ex) {
        try {
          String query = "select * from " + table
              + " order by timestamp limit 1";
          log.debug("Query: " + query);
          rs = db.query(query);
          if (rs.next()) {
            ResultSetMetaData rmeta = rs.getMetaData();
            for (int i = 1; i <= rmeta.getColumnCount(); i++) {
              if (rmeta.getColumnName(i).toLowerCase().equals("timestamp")) {
                start = rs.getTimestamp(i).getTime();
              }
            }
          }
          end = start + (interval * 60000);
        } catch (SQLException ex2) {
          log.error("Unable to determine starting point in table: "
              + this.table);
          log.error("SQL Error:" + ExceptionUtil.getStackTrace(ex2));
          return;
        }
      }
      try {
        ResultSetMetaData rmeta = rs.getMetaData();
        int col = rmeta.getColumnCount();
        columns = new String[col];
        columnsType = new int[col];
        for (int i = 1; i <= col; i++) {
          columns[i - 1] = rmeta.getColumnName(i);
          columnsType[i - 1] = rmeta.getColumnType(i);
        }

        for (int i = 0; i < columns.length; i++) {
          if (i == 0) {
            fields = columns[i];
            if (columnsType[i] == java.sql.Types.VARCHAR) {
              if (groupBy.equals("")) {
                groupBy = " group by " + columns[i];
              } else {
                groupBy = groupBy + "," + columns[i];
              }
            }
          } else {
            if (columnsType[i] == java.sql.Types.VARCHAR
                || columnsType[i] == java.sql.Types.TIMESTAMP) {
              fields = fields + "," + columns[i];
              if (columnsType[i] == java.sql.Types.VARCHAR) {
                if (groupBy.equals("")) {
                  groupBy = " group by " + columns[i];
                } else {
                  groupBy = groupBy + "," + columns[i];
                }
              }
            } else {
              fields = fields + ",AVG(" + columns[i] + ") as " + columns[i];
            }
          }
        }
      } catch (SQLException ex) {
        log.error("SQL Error:" + ExceptionUtil.getStackTrace(ex));
        return;
      }
      if (groupBy.equals("")) {
        emptyPrimeKey = true;
      }
      long previousStart = start;
      long partition = 0;
      String timeWindowType = "week";
      while (end < now.getTimeInMillis() - (interval * 2 * 60000)) {
        // Select new data sample for the given intervals
        if (interval == 5) {
          timeWindowType = "month";
          partition = start / DatabaseConfig.MONTH;
        } else if (interval == 30) {
          timeWindowType = "quarter";
          partition = start / DatabaseConfig.QUARTER;
        } else if (interval == 180) {
          timeWindowType = "year";
          partition = start / DatabaseConfig.YEAR;
        } else if (interval == 720) {
          timeWindowType = "decade";
          partition = start / DatabaseConfig.DECADE;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startS = formatter.format(start);
        String endS = formatter.format(end);
        dateclause = "Timestamp >= '" + startS + "' and Timestamp <= '" + endS
            + "'";
        if (emptyPrimeKey) {
          groupBy = " group by FLOOR(UNIX_TIMESTAMP(TimeStamp)/" + interval
              * 60 + ")";
        }
        String query = "replace into " + this.table + "_" + partition + "_"
            + timeWindowType + " (select " + fields + " from " + table
            + " where " + dateclause + groupBy + ")";
        log.debug(query);
        db.execute(query);
        if (previousStart == start) {
          start = start + (interval * 60000);
          end = start + (interval * 60000);
          previousStart = start;
        }
      }
      db.close();
    }
  }
View Full Code Here

  String cluster = "demo";
  long current = Calendar.getInstance().getTimeInMillis();

  public void setUp() {
    System.setProperty("CLUSTER","demo");
    DatabaseWriter db = new DatabaseWriter(cluster);
    String buffer = "";
    File aFile = new File(System.getenv("CHUKWA_CONF_DIR")
                 + File.separator + "database_create_tables.sql");
    buffer = readFile(aFile);
    String tables[] = buffer.split(";");
    for(String table : tables) {
      if(table.length()>5) {
        db.execute(table);
      }
    }
    db.close();
    for(int i=0;i<timeWindow.length;i++) {
      TableCreator tc = new TableCreator();
      long start = current;
      long end = current + (timeWindow[i]*1440*60*1000);
      tc.createTables(start, end);
View Full Code Here

      tc.createTables(start, end);
    }
  }

  public void tearDown() {
    DatabaseWriter db = null;
    try {
      db = new DatabaseWriter(cluster);
      ResultSet rs = db.query("show tables");
      ArrayList<String> list = new ArrayList<String>();
      while(rs.next()) {
        String table = rs.getString(1);
        list.add(table);
      }
      for(String table : list) {
        db.execute("drop table "+table);
      }
    } catch(Throwable ex) {
    } finally {
      if(db!=null) {
        db.close();
      }
    }
  }
View Full Code Here

    ChukwaConfiguration cc = new ChukwaConfiguration();
    String query = "select * from ["+table+"];";
    Macro mp = new Macro(current,query);
    query = mp.toString();
    try {
      DatabaseWriter db = new DatabaseWriter(cluster);
      ResultSet rs = db.query(query);
      while(rs.next()) {
        int i = 1;
        String value = rs.getString(i);
      }
      db.close();
    } catch(SQLException ex) {
      fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
    }
  }
View Full Code Here

        de.dropTables(start, end);
      } catch(Throwable ex) {
        fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
      }
      assertTrue("DataExpiration executed.", true);
      DatabaseWriter db = null;
      try {
        db = new DatabaseWriter(cluster);
        String query = "select * from [system_metrics];";
        Macro mp = new Macro(current,query);
        query = mp.toString();
        ResultSet rs = db.query(query);
      } catch(SQLException ex) {
        assertTrue("Table is not suppose to exist.",true);
        db.close();
      }
    }
  }
View Full Code Here

  String cluster = "demo";
  long current = Calendar.getInstance().getTimeInMillis();

  public void setUp() {
    System.setProperty("CLUSTER","demo");
    DatabaseWriter db = new DatabaseWriter(cluster);
    String buffer = "";
    File aFile = new File(System.getenv("CHUKWA_CONF_DIR")
                 + File.separator + "database_create_tables.sql");
    buffer = readFile(aFile);
    String tables[] = buffer.split(";");
    for(String table : tables) {
      if(table.length()>5) {
        db.execute(table);
      }
    }
    db.close();
    for(int i=0;i<timeWindow.length;i++) {
      TableCreator tc = new TableCreator();
      long start = current;
      long end = current + (timeWindow[i]*1440*60*1000);
      tc.createTables(start, end);
View Full Code Here

      tc.createTables(start, end);
    }
  }

  public void tearDown() {
    DatabaseWriter db = null;
    try {
      db = new DatabaseWriter(cluster);
      ResultSet rs = db.query("show tables");
      ArrayList<String> list = new ArrayList<String>();
      while(rs.next()) {
        String table = rs.getString(1);
        list.add(table);
      }
      for(String table : list) {
        db.execute("drop table "+table);
      }
    } catch(Throwable ex) {
    } finally {
      if(db!=null) {
        db.close();
      }
    }
  }
View Full Code Here

    ChukwaConfiguration cc = new ChukwaConfiguration();
    String query = "select * from ["+table+"];";
    Macro mp = new Macro(current,query);
    query = mp.toString();
    try {
      DatabaseWriter db = new DatabaseWriter(cluster);
      ResultSet rs = db.query(query);
      while(rs.next()) {
        int i = 1;
        String value = rs.getString(i);
      }
      db.close();
    } catch(SQLException ex) {
      fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
    }
  }
View Full Code Here

  }

  public void testTableCreator() {
    for(int i=0;i<timeWindow.length;i++) {
      try {
        DatabaseWriter db = new DatabaseWriter(cluster);
        for(String table : tables) {
          String query = "select * from ["+table+"];";
          Macro mp = new Macro(current,query);
          query = mp.toString();
          ResultSet rs = db.query(query);
          rs.last();
          int count = rs.getRow();
          assertTrue("Table should exist and return empty result.", count==0);
        }
        db.close();
      } catch(SQLException ex) {
        fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.hadoop.chukwa.util.DatabaseWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.