Search in sources :

Example 11 with QueryOptions

use of com.datastax.driver.core.QueryOptions in project gora by apache.

the class CassandraClient method populateQueryOptions.

private Cluster.Builder populateQueryOptions(Properties properties, Cluster.Builder builder) {
    String consistencyLevelProp = properties.getProperty(CassandraStoreParameters.CONSISTENCY_LEVEL);
    String serialConsistencyLevelProp = properties.getProperty(CassandraStoreParameters.SERIAL_CONSISTENCY_LEVEL);
    String fetchSize = properties.getProperty(CassandraStoreParameters.FETCH_SIZE);
    QueryOptions options = new QueryOptions();
    if (consistencyLevelProp != null) {
        options.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevelProp));
    }
    if (serialConsistencyLevelProp != null) {
        options.setSerialConsistencyLevel(ConsistencyLevel.valueOf(serialConsistencyLevelProp));
    }
    if (fetchSize != null) {
        options.setFetchSize(Integer.parseInt(fetchSize));
    }
    return builder.withQueryOptions(options);
}
Also used : QueryOptions(com.datastax.driver.core.QueryOptions)

Example 12 with QueryOptions

use of com.datastax.driver.core.QueryOptions in project spring-boot by spring-projects.

the class CassandraAutoConfiguration method getQueryOptions.

private QueryOptions getQueryOptions() {
    QueryOptions options = new QueryOptions();
    CassandraProperties properties = this.properties;
    if (properties.getConsistencyLevel() != null) {
        options.setConsistencyLevel(properties.getConsistencyLevel());
    }
    if (properties.getSerialConsistencyLevel() != null) {
        options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel());
    }
    options.setFetchSize(properties.getFetchSize());
    return options;
}
Also used : QueryOptions(com.datastax.driver.core.QueryOptions)

Example 13 with QueryOptions

use of com.datastax.driver.core.QueryOptions in project cas by apereo.

the class DefaultCassandraSessionFactory method initializeCassandraCluster.

private static Cluster initializeCassandraCluster(final BaseCassandraProperties cassandra) {
    final Cluster cluster;
    final PoolingOptions poolingOptions = new PoolingOptions().setMaxRequestsPerConnection(HostDistance.LOCAL, cassandra.getMaxRequestsPerConnection()).setConnectionsPerHost(HostDistance.LOCAL, cassandra.getCoreConnections(), cassandra.getMaxConnections());
    final DCAwareRoundRobinPolicy.Builder dcPolicyBuilder = DCAwareRoundRobinPolicy.builder();
    if (StringUtils.isNotBlank(cassandra.getLocalDc())) {
        dcPolicyBuilder.withLocalDc(cassandra.getLocalDc());
    }
    final TokenAwarePolicy loadBalancingPolicy = new TokenAwarePolicy(dcPolicyBuilder.build(), cassandra.isShuffleReplicas());
    final SocketOptions socketOptions = new SocketOptions().setConnectTimeoutMillis(cassandra.getConnectTimeoutMillis()).setReadTimeoutMillis(cassandra.getReadTimeoutMillis());
    final QueryOptions queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(cassandra.getConsistencyLevel())).setSerialConsistencyLevel(ConsistencyLevel.valueOf(cassandra.getSerialConsistencyLevel()));
    final RetryPolicy retryPolicy = RetryPolicyType.valueOf(cassandra.getRetryPolicy()).getRetryPolicy();
    final Cluster.Builder builder = Cluster.builder().withCredentials(cassandra.getUsername(), cassandra.getPassword()).withPoolingOptions(poolingOptions).withProtocolVersion(ProtocolVersion.valueOf(cassandra.getProtocolVersion())).withLoadBalancingPolicy(loadBalancingPolicy).withSocketOptions(socketOptions).withRetryPolicy(new LoggingRetryPolicy(retryPolicy)).withCompression(ProtocolOptions.Compression.valueOf(cassandra.getCompression())).withPort(cassandra.getPort()).withQueryOptions(queryOptions);
    Arrays.stream(StringUtils.split(cassandra.getContactPoints(), ',')).forEach(contactPoint -> builder.addContactPoint(StringUtils.trim(contactPoint)));
    cluster = builder.build();
    if (LOGGER.isDebugEnabled()) {
        cluster.getMetadata().getAllHosts().forEach(clusterHost -> LOGGER.debug("Host [{}]:\n\n\tDC: [{}]\n\tRack: [{}]\n\tVersion: [{}]\n\tDistance: [{}]\n\tUp?: [{}]\n", clusterHost.getAddress(), clusterHost.getDatacenter(), clusterHost.getRack(), clusterHost.getCassandraVersion(), loadBalancingPolicy.distance(clusterHost), clusterHost.isUp()));
    }
    return cluster;
}
Also used : DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) QueryOptions(com.datastax.driver.core.QueryOptions) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy)

Example 14 with QueryOptions

use of com.datastax.driver.core.QueryOptions in project ats-framework by Axway.

the class CassandraDbProvider method connect.

/**
 * Currently we connect just once and then reuse the connection.
 * We do not bother with closing the connection.
 *
 * It is normal to use one Session per DB. The Session is thread safe.
 */
private void connect() {
    if (cluster == null) {
        log.info("Connecting to Cassandra server on " + this.dbHost + " at port " + this.dbPort);
        // allow fetching as much data as present in the DB
        QueryOptions queryOptions = new QueryOptions();
        queryOptions.setFetchSize(Integer.MAX_VALUE);
        queryOptions.setConsistencyLevel(ConsistencyLevel.ONE);
        cluster = Cluster.builder().addContactPoint(this.dbHost).withPort(this.dbPort).withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 30000)).withQueryOptions(queryOptions).withCredentials(this.dbUser, this.dbPassword).build();
    }
    if (session == null) {
        log.info("Connecting to Cassandra DB with name " + this.dbName);
        session = cluster.connect(dbName);
    }
}
Also used : ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) QueryOptions(com.datastax.driver.core.QueryOptions) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy)

Example 15 with QueryOptions

use of com.datastax.driver.core.QueryOptions in project cassandra by apache.

the class PagingTest method checkDuplicates.

private void checkDuplicates(String message) throws InterruptedException {
    // sometimes one node doesn't have time come up properly?
    Thread.sleep(5000);
    try (com.datastax.driver.core.Cluster c = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").withProtocolVersion(ProtocolVersion.V3).withQueryOptions(new QueryOptions().setFetchSize(101)).build();
        Session s = c.connect()) {
        Statement stmt = new SimpleStatement("select distinct token(pk) from " + DistributedTestBase.KEYSPACE + ".tbl WHERE token(pk) > " + Long.MIN_VALUE + " AND token(pk) < " + Long.MAX_VALUE);
        stmt.setConsistencyLevel(com.datastax.driver.core.ConsistencyLevel.ALL);
        ResultSet res = s.execute(stmt);
        Set<Object> seenTokens = new HashSet<>();
        Iterator<Row> rows = res.iterator();
        Set<Object> dupes = new HashSet<>();
        while (rows.hasNext()) {
            Object token = rows.next().getObject(0);
            if (seenTokens.contains(token))
                dupes.add(token);
            seenTokens.add(token);
        }
        assertEquals(message + ": too few rows", 5000, seenTokens.size());
        assertTrue(message + ": dupes is not empty", dupes.isEmpty());
    }
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) SimpleStatement(com.datastax.driver.core.SimpleStatement) QueryOptions(com.datastax.driver.core.QueryOptions) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) HashSet(java.util.HashSet)

Aggregations

QueryOptions (com.datastax.driver.core.QueryOptions)17 Cluster (com.datastax.driver.core.Cluster)7 SocketOptions (com.datastax.driver.core.SocketOptions)6 PoolingOptions (com.datastax.driver.core.PoolingOptions)5 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)5 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)4 PlainTextAuthProvider (com.datastax.driver.core.PlainTextAuthProvider)3 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)3 ExponentialReconnectionPolicy (com.datastax.driver.core.policies.ExponentialReconnectionPolicy)3 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)3 SSLOptions (com.datastax.driver.core.SSLOptions)2 WhiteListPolicy (com.datastax.driver.core.policies.WhiteListPolicy)2 InetSocketAddress (java.net.InetSocketAddress)2 Test (org.junit.Test)2 AuthProvider (com.datastax.driver.core.AuthProvider)1 Configuration (com.datastax.driver.core.Configuration)1 Host (com.datastax.driver.core.Host)1 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)1 Metadata (com.datastax.driver.core.Metadata)1 ResultSet (com.datastax.driver.core.ResultSet)1