Java Code Examples for com.datastax.driver.core.Cluster#newSession()

The following examples show how to use com.datastax.driver.core.Cluster#newSession() . 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: DefaultCommandContext.java    From titus-control-plane with Apache License 2.0 7 votes vote down vote up
public static CommandContext newCommandContext(CommandLine commandLine) {
    List<String> ips = StringExt.splitByComma(commandLine.getOptionValue("H"));
    int sourcePort = Integer.parseInt(commandLine.getOptionValue("p"));

    QueryOptions queryOptions = new QueryOptions()
            .setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);

    Cluster cluster = Cluster.builder()
            .addContactPoints((String[]) ips.toArray())
            .withPort(sourcePort)
            .withQueryOptions(queryOptions)
            .build();

    return new DefaultCommandContext(
            commandLine,
            cluster.newSession(),
            sourceKeySpace -> cluster.connect('"' + sourceKeySpace + '"'),
            targetKeySpace -> cluster.connect('"' + targetKeySpace + '"')
    ) {
        @Override
        public void shutdown() {
            cluster.close();
        }
    };
}
 
Example 2
Source File: CassandraIO.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the list of token ranges that a table occupies on a give Cassandra node.
 *
 * <p>NB: This method is compatible with Cassandra 2.1.5 and greater.
 */
private static List<TokenRange> getTokenRanges(Cluster cluster, String keyspace, String table) {
  try (Session session = cluster.newSession()) {
    ResultSet resultSet =
        session.execute(
            "SELECT range_start, range_end, partitions_count, mean_partition_size FROM "
                + "system.size_estimates WHERE keyspace_name = ? AND table_name = ?",
            keyspace,
            table);

    ArrayList<TokenRange> tokenRanges = new ArrayList<>();
    for (Row row : resultSet) {
      TokenRange tokenRange =
          new TokenRange(
              row.getLong("partitions_count"),
              row.getLong("mean_partition_size"),
              new BigInteger(row.getString("range_start")),
              new BigInteger(row.getString("range_end")));
      tokenRanges.add(tokenRange);
    }
    // The table may not contain the estimates yet
    // or have partitions_count and mean_partition_size fields = 0
    // if the data was just inserted and the amount of data in the table was small.
    // This is very common situation during tests,
    // when we insert a few rows and immediately query them.
    // However, for tiny data sets the lack of size estimates is not a problem at all,
    // because we don't want to split tiny data anyways.
    // Therefore, we're not issuing a warning if the result set was empty
    // or mean_partition_size and partitions_count = 0.
    return tokenRanges;
  }
}
 
Example 3
Source File: DockerCassandra.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void grantPermissionToTestingUser(Cluster privilegedCluster, String keyspace) {
    try (Session session = privilegedCluster.newSession()) {
        session.execute("GRANT CREATE ON KEYSPACE " + keyspace + " TO " + CASSANDRA_TESTING_USER);
        session.execute("GRANT SELECT ON KEYSPACE " + keyspace + " TO " + CASSANDRA_TESTING_USER);
        session.execute("GRANT MODIFY ON KEYSPACE " + keyspace + " TO " + CASSANDRA_TESTING_USER);
        // some tests require dropping in setups
        session.execute("GRANT DROP ON KEYSPACE " + keyspace + " TO " + CASSANDRA_TESTING_USER);
    }
}
 
Example 4
Source File: WebDriverSetup.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static WebDriverSetup createSetup(boolean shared) throws Exception {
    int uiPort = getAvailablePort();
    File testDir = Files.createTempDir();
    CentralModule centralModule;
    Container container;
    if (useCentral) {
        CassandraWrapper.start();
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        Session session = new Session(cluster.newSession(), "glowroot_unit_tests", null,
                PoolingOptions.DEFAULT_MAX_QUEUE_SIZE);
        session.updateSchemaWithRetry("drop table if exists agent_config");
        session.updateSchemaWithRetry("drop table if exists user");
        session.updateSchemaWithRetry("drop table if exists role");
        session.updateSchemaWithRetry("drop table if exists central_config");
        session.updateSchemaWithRetry("drop table if exists agent");
        session.close();
        cluster.close();
        int grpcPort = getAvailablePort();
        centralModule = createCentralModule(uiPort, grpcPort);
        container = createContainerReportingToCentral(grpcPort, testDir);
    } else {
        centralModule = null;
        container = createContainer(uiPort, testDir);
    }
    if (SauceLabs.useSauceLabs()) {
        return new WebDriverSetup(centralModule, container, uiPort, shared, null);
    } else {
        return new WebDriverSetup(centralModule, container, uiPort, shared, createWebDriver());
    }
}
 
Example 5
Source File: DockerCassandra.java    From james-project with Apache License 2.0 4 votes vote down vote up
private void provisionNonPrivilegedUser(Cluster privilegedCluster) {
    try (Session session = privilegedCluster.newSession()) {
        session.execute("CREATE ROLE IF NOT EXISTS " + CASSANDRA_TESTING_USER + " WITH PASSWORD = '" + CASSANDRA_TESTING_PASSWORD + "' AND LOGIN = true");
    }
}