Package com.datastax.driver.core

Examples of com.datastax.driver.core.Session


  }

  protected void executeSpecsAndScripts(@SuppressWarnings("rawtypes") List specs, List<String> scripts) {

    Session system = null;
    CqlTemplate template = null;

    try {
      if (specs != null) {
        system = specs.size() == 0 ? null : cluster.connect();
        template = system == null ? null : new CqlTemplate(system);

        Iterator<?> i = specs.iterator();
        while (i.hasNext()) {
          KeyspaceActionSpecification<?> spec = (KeyspaceActionSpecification<?>) i.next();
          String cql = (spec instanceof CreateKeyspaceSpecification) ? new CreateKeyspaceCqlGenerator(
              (CreateKeyspaceSpecification) spec).toCql() : new DropKeyspaceCqlGenerator(
              (DropKeyspaceSpecification) spec).toCql();

          template.execute(cql);
        }
      }

      if (scripts != null) {

        if (system == null) {
          system = scripts.size() == 0 ? null : cluster.connect();
        }

        if (template == null) {
          template = system == null ? null : new CqlTemplate(system);
        }

        for (String script : scripts) {

          if (log.isDebugEnabled()) {
            log.debug("executing raw CQL [{}]", script);
          }

          template.execute(script);
        }
      }
    } finally {

      if (system != null) {
        system.close();
      }
    }
  }
View Full Code Here


    String keyspace = getKeyspaceName();
    if (!StringUtils.hasText(keyspace)) {
      return;
    }

    Session system = cluster().getObject().connect();
    KeyspaceMetadata kmd = system.getCluster().getMetadata().getKeyspace(keyspace);
    if (kmd != null) {
      return;
    }

    system.execute(toCql(createKeyspace().name(keyspace).withSimpleReplication()));
    system.close();
  }
View Full Code Here

  }

  private void createKeyspace(String node, String keyspace) throws Exception {

    Session session = null;
    Exception error = null;
    try {
      _log.info("Started checking whether keyspace {} exists...",
          keyspace);
      session = _rootCluster.connect(keyspace);
      _log.info("Finished checking whether keyspace {} exists...",
          keyspace);
      _log.info("Keyspace {} exists, nothing to do.", keyspace);
    }
    // Keyspace does not exist, so session is not null...
    catch (AlreadyExistsException ae) {
    }
    // Keyspace does not exist, so we try to create it.
    // In that case, session is null
    catch (InvalidQueryException ae) {
    }
    // Unknown exception -> error
    catch (Exception e) {
      _log.error("Unexcpected Exception when checking for keyspace {}!",
          keyspace, e);
      error = e;
    }

    if (session == null && error == null) {
      _log.info("Finished checking whether keyspace {} exists...",
          keyspace);
      _log.info("Keyspace does not yet {} exist, trying to create it",
          keyspace);
      try {
        _log.info("Started creating keyspace {} ...", keyspace);
        session = _rootCluster.connect();

        final String query = String.format("" + "CREATE keyspace %s "
            + "WITH replication = "
            + "{'class': 'SimpleStrategy', "
            + "'replication_factor' : 3}", keyspace);
        session.execute(query);
        _log.info("Finished creating keyspace {} .", keyspace);
      } catch (Exception e) {
        _log.error("Error creating keyspace {}", keyspace, e);
        error = e;
      }
      // In any case, shut down the session for the root user.
      finally {
        if (session != null) {
          _log.info("Started shutting down session for creating keyspace ...");
          session.shutdown();
          _log.info("Finished shutting down session for creating keyspace.");
        }
      }
    }
    if (error != null) {
View Full Code Here

  }

  private void createTable(VirtualDataWindowContext context) throws Exception {

    Session session = null;
    Exception error = null;
    final String table = context.getNamedWindowName();
    final String keyspace = context.getViewFactoryContext()
        .getNamespaceName();

    boolean tableExists = true;
    try {
      _log.info("Started checking whether table {} exists...", table);
      session = _rootCluster.connect(keyspace);
      session.execute(String.format("SELECT * FROM %s LIMIT 10", table));

      _log.info("Finished checking whether table {} exists...", table);
      _log.info("Table {} exists, nothing to do.", table);
    }
    // Keyspace does not exist, so we try to create it.
    catch (InvalidQueryException ae) {
      tableExists = false;
    }
    // Unknown exception -> error
    catch (Exception e) {
      tableExists = false;
      _log.error("Unexcpected Exception when checking for table {}!",
          table, e);
      error = e;
    }

    if (error == null) {
      _log.info("Finished checking whether table {} exists...", table);
      try {
        session = _rootCluster.connect(keyspace);
        if (!tableExists) {
          _log.info(
              "Table does not yet {} exist, trying to create it",
              table);

          try {
            _log.info("Started creating table {} ...", table);
            final String createQuery = QueryStringBuilder
                .createCreateTableQuery(context);
            _log.debug("Executing create query: {}", createQuery);
            session.execute(createQuery);
            _log.info("Finished creating table {} .", table);
          } catch (Exception e) {
            _log.error("Error creating table {} .", table);
            error = e;
          }
        }

        if (_username != null && error == null) {
          try {
            _log.info("Started granting rights to table {} ...",
                table);
            final String grantQuery = String.format(
                "GRANT ALL on %s TO %s", table, _username);
            _log.debug("Executing grant query: {}", grantQuery);
            session.execute(grantQuery);
            _log.info("Finished granting rights to table {}.",
                table);
          } catch (Exception e) {
            _log.error("Error granting rights to table {}.", table,
                e);
            error = e;
          }
        }

      } catch (Exception e) {
        _log.error("Error creating table {}", table, e);
        error = e;
      }
      // In any case, shut down the session for the root user.
      finally {
        if (session != null) {
          _log.info("Started shutting down session for creating table ...");
          session.shutdown();
          _log.info("Finished shutting down session for creating table.");
        }
      }
    }
    if (error != null) {
View Full Code Here

    }
    CassandraVirtualDataWindow result = null;
    synchronized (_cluster) {
      try {
        _log.info("Started connecting to cluster...");
        final Session session = _cluster.connect(_configuration
            .getKeyspace());
        _log.info("Finished connecting to cluster...");

        createTable(context);
View Full Code Here

    }
    CassandraVirtualDataWindow result = null;
    synchronized (_cluster) {
      try {
        _log.info("Started connecting to cluster...");
        final Session session = _cluster.connect(_configuration
            .getKeyspace());
        _log.info("Finished connecting to cluster...");

        result = new CassandraVirtualDataWindow(session,
            _configuration.getTable(), context);
View Full Code Here

        options.setFetchSize(fetchSize);
        options.setConsistencyLevel(consistencyLevel);
        clusterBuilder.withQueryOptions(options);

        Cluster cluster = clusterBuilder.build();
        Session session = cluster.connect();
        return new CassandraSession(connectorId.toString(), session, fetchSizeForPartitionKeySelect, limitForPartitionKeySelect);
    }
View Full Code Here

      throw new RuntimeException(
          "Cassandra cluster has not yet been initialized!");
    }
    try {
      _log.info("Started connecting to cluster...");
      final Session session = _cluster.connect(_configuration
          .getKeyspace());
      _log.info("Finished connecting to cluster...");
      return new CassandraVirtualDataWindow(session, context);
    } catch (Exception e) {
      _log.error("Error connecting to cluster!");
View Full Code Here

    }


    public void truncateTable(String keyspaceName, String tableName) {
        String query = "TRUNCATE " + tableName;
        Session session = REPOSITORY.getSessionForKeyspace(keyspaceName);
        session.execute(new SimpleStatement(query).setConsistencyLevel(com.datastax.driver.core.ConsistencyLevel.ALL));
        DML_LOGGER.debug("{} : [{}] with CONSISTENCY LEVEL [{}]", "  Simple query", query, "ALL");
    }
View Full Code Here

        createKeyspaceIfNeeded(cluster, keyspaceName, keyspaceDurableWrite);

        if (nativeSessionOnly) {
            REPOSITORY.addNewSessionToKeyspace(keyspaceName, cluster.connect(keyspaceName));
        } else {
            Session nativeSession = cluster.connect(keyspaceName);
            achillesParameters.put(ConfigurationParameters.NATIVE_SESSION, nativeSession);
            achillesParameters.put(ConfigurationParameters.KEYSPACE_NAME, keyspaceName);
            if (!achillesParameters.containsKey(ConfigurationParameters.FORCE_TABLE_CREATION)) {
                achillesParameters.put(ConfigurationParameters.FORCE_TABLE_CREATION, true);
            }
View Full Code Here

TOP

Related Classes of com.datastax.driver.core.Session

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.