Exemple #1
0
  public void press() {
    // get a list of all the tables and columns
    logger.info("Starting MySQL to Mongo Conversion...");
    logger.info("Preparing Tables...");
    mySQLHandler.initialiseDatabase();
    List<Table> tables = mySQLHandler.getTableList();
    for (int i = 0; i < tables.size(); i++) {
      Table table = tables.get(i);
      List<Column> columns = table.getColumns();
      List<DBObject> dboList = new ArrayList<DBObject>();
      SqlRowSet rs = mySQLHandler.selectAllFromTable(table.getTableName());
      logger.info("Creating objects for " + table.getTableName() + "...");
      while (rs.next()) {
        BasicDBObject dbo = new BasicDBObject();
        for (int j = 0; j < columns.size(); j++) {
          Column col = columns.get(j);
          String colName = col.getColumnName();
          switch (col.getType()) {
            case Types.INTEGER:
            case Types.BIGINT:
              dbo.append(colName, rs.getInt(colName));
              break;
            case Types.DOUBLE:
              dbo.append(colName, rs.getDouble(colName));
              break;
            case Types.DATE:
              dbo.append(colName, rs.getDate(colName));
              break;
            default:
              dbo.append(colName, rs.getString(colName));
              break;
          }
        }
        dboList.add(dbo);
      }

      // now insert it
      logger.info(
          "Inserting " + dboList.size() + " mongo rows into " + table.getTableName() + "...");
      table.setNumOfRows(dboList.size());
      try {
        mongoHandler.createCollection(table.getTableName(), true);
        mongoHandler.batchInsert(dboList, table.getTableName());
        assert (mongoHandler.getNumObjectsInCollection(table.getTableName()) == dboList.size());
        logger.info(table.getTableName() + " DONE!");
      } catch (CollectionExistException e) {
        e.printStackTrace();
      }
    }
    logger.info(tables.size() + " collections added!");

    // now check go get it from mongo and check if the data there is correct
    logger.info("Checking mongo consistency...");
    for (int i = 0; i < tables.size(); i++) {
      Table table = tables.get(i);
      assert (mongoHandler.getNumObjectsInCollection(table.getTableName()) == table.getNumOfRows());
      logger.info(table.getTableName() + " consistent!");
    }
    logger.info("MySQL to Mongo Conversion Completed!!!!");
  }