Java Code Examples for com.datastax.driver.core.Session#getCluster()

The following examples show how to use com.datastax.driver.core.Session#getCluster() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private static void migrate(
    int dbVersion,
    MigrationRepository repository,
    Session session) {
  Preconditions.checkState(dbVersion < repository.getLatestVersion());

  for (int i = dbVersion + 1 ; i <= repository.getLatestVersion(); ++i) {
    final int nextVersion = i;
    // perform the migrations one at a time, so the MigrationXXX classes can be executed alongside the scripts
    MigrationRepository migrationRepo = new MigrationRepository("db/cassandra") {
      @Override
      public int getLatestVersion() {
        return nextVersion;
      }

      @Override
      public List getMigrationsSinceVersion(int version) {
        return Collections.singletonList((Object)super.getMigrationsSinceVersion(nextVersion - 1).get(0));
      }
    };

    try (Database database = new Database(session.getCluster(), session.getLoggedKeyspace())) {
      MigrationTask migration = new MigrationTask(database, migrationRepo, true);
      migration.migrate();
      // after the script execute any MigrationXXX class that exists with the same version number
      Class.forName("io.cassandrareaper.storage.cassandra.Migration" + String.format("%03d", nextVersion))
          .getDeclaredMethod("migrate", Session.class)
          .invoke(null, session);

      LOG.info("executed Migration" + String.format("%03d", nextVersion));
    } catch (ReflectiveOperationException ignore) { }
    LOG.info(String.format("Migrated keyspace %s to version %d", session.getLoggedKeyspace(), nextVersion));
  }
}
 
Example 2
Source File: CassandraHelper.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** Closes Cassandra driver session. */
public static void closeSession(Session driverSes) {
    if (driverSes == null)
        return;

    Cluster cluster = driverSes.getCluster();

    if (!driverSes.isClosed())
        U.closeQuiet(driverSes);

    if (!cluster.isClosed())
        U.closeQuiet(cluster);
}
 
Example 3
Source File: CassandraSessionFactory.java    From jmeter-cassandra with Apache License 2.0 5 votes vote down vote up
public static synchronized void destroyClusters() {
    for (Session session : instance.sessions.values()) {
        Cluster cluster = session.getCluster();
        session.close();
        cluster.close();
    }
    instance.sessions.clear();
}
 
Example 4
Source File: DecribeOperation.java    From Explorer with Apache License 2.0 5 votes vote down vote up
/**
 * execute shCQLcoomand
 * @param Session session
 * @param shCQLcommand command shCQL
 * @return List string with result
 */
@Override
public Table execute(Session session, String shCQLcommand) {

    SHCQLOperation shCqlOperation = new SHCQLOperation(shCQLcommand);
    Cluster cluster = session.getCluster();
    DescribeExecutor executor = DescribeExecutorFactory.select(shCqlOperation.identifier());
    executor.optionalParam(shCqlOperation.optionalValue());
    return executor.execute(cluster.getMetadata());
}
 
Example 5
Source File: ExecutionEngine.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public void execute() throws CommandExecutionException, XMLStreamException, ChecksumInvalidException {
   try {
      if(!metadataTablesExist()) {
         createMetadataTables();
      }

      ChangeLogSet xmlChanges = loadXML();
      ChangeLogSet dbState = loadDB();

      try {
      	xmlChanges.verify(dbState);
      }
      catch(ChecksumInvalidException e) {
      	if(context.getManagerContext().isAuto()) {
      		throw e;
      	}
      	else {
       	System.out.println("Invalid checksum: " + e.getMessage());
       	System.out.println("WARNING: This schema appears to be corrupt, continuing execution could result in data loss, are you sure you want to continue?");
       	if(promptToContinue()) {
       		xmlChanges = loadXML();
       		xmlChanges.verify(dbState, false);
       	}
       	else {
       		System.exit(0);
       	}
      	}
      }

      List<ChangeLog> changesToApply = identifyChanges(xmlChanges, dbState);

      dumpChanges(changesToApply);

      if(!promptToContinue()) {
         System.exit(0);
      }

      List<ExecutionCommand> commands = new LinkedList<ExecutionCommand>();
      for(ChangeLog cl : changesToApply) {
         commands.add(new ChangeLogExecutionCommand(cl));
      }
      commands.add(new ImportLocalizationKeysCommand());

      System.out.println("\nExecuting...");

      executeCommands(commands);

      // if we've rolled back make sure that the target version is now the latest version installed
      if(context.getManagerContext().getOperation() == Operation.ROLLBACK) {
         VersionHistory history = new VersionHistory();
         history.setStatus(Status.APPLIED);
         history.setTimestamp(new Date());
         history.setUsername(context.getManagerContext().getProfile().getUsername());
         history.setVersion(context.getManagerContext().getRollbackTarget());
         context.getVersionHistoryDAO().insert(history);
      }


      System.out.println("...Done!");

   } finally {
      Session session = context.getSession();
      if(session != null) {
         Cluster cluster = session.getCluster();
         session.close();
         cluster.close();
      }
   }
}
 
Example 6
Source File: Sessions.java    From glowroot with Apache License 2.0 4 votes vote down vote up
static void closeSession(Session session) {
    Cluster cluster = session.getCluster();
    session.close();
    cluster.close();
}