Search in sources :

Example 76 with Cluster

use of com.datastax.driver.core.Cluster in project jhipster-sample-app-cassandra by jhipster.

the class CassandraConfiguration method cluster.

@Bean
public Cluster cluster(CassandraProperties properties) {
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withProtocolVersion(protocolVersion).withPort(getPort(properties));
    if (properties.getUsername() != null) {
        builder.withCredentials(properties.getUsername(), properties.getPassword());
    }
    if (properties.getCompression() != null) {
        builder.withCompression(properties.getCompression());
    }
    if (properties.getLoadBalancingPolicy() != null) {
        LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
        builder.withLoadBalancingPolicy(policy);
    }
    builder.withQueryOptions(getQueryOptions(properties));
    if (properties.getReconnectionPolicy() != null) {
        ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
        builder.withReconnectionPolicy(policy);
    }
    if (properties.getRetryPolicy() != null) {
        RetryPolicy policy = instantiate(properties.getRetryPolicy());
        builder.withRetryPolicy(policy);
    }
    builder.withSocketOptions(getSocketOptions(properties));
    if (properties.isSsl()) {
        builder.withSSL();
    }
    builder.addContactPoints(StringUtils.toStringArray(properties.getContactPoints()));
    Cluster cluster = builder.build();
    TupleType tupleType = cluster.getMetadata().newTupleType(DataType.timestamp(), DataType.varchar());
    cluster.getConfiguration().getCodecRegistry().register(LocalDateCodec.instance).register(InstantCodec.instance).register(new ZonedDateTimeCodec(tupleType));
    if (metricRegistry != null) {
        cluster.init();
        metricRegistry.registerAll(cluster.getMetrics().getRegistry());
    }
    return cluster;
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) TupleType(com.datastax.driver.core.TupleType) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) ZonedDateTimeCodec(com.datastax.driver.extras.codecs.jdk8.ZonedDateTimeCodec) Bean(org.springframework.context.annotation.Bean)

Example 77 with Cluster

use of com.datastax.driver.core.Cluster in project microservices by pwillhan.

the class CoreConceptsApplication method main.

public static void main(String[] args) {
    ConfigurableApplicationContext ctx = SpringApplication.run(CoreConceptsApplication.class, args);
    Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
    Session session = cluster.connect("cwt");
    Select selectStatement = QueryBuilder.select().column("username").column("email").column("fullname").column("password").from(// database (keyspace): home_security  table: home
    "member");
    ResultSet resultSet = session.execute(selectStatement);
    for (Row row : resultSet) {
        System.out.println(row.getString("fullname").toString());
    }
    session.close();
    cluster.close();
    ctx.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session)

Example 78 with Cluster

use of com.datastax.driver.core.Cluster in project data-transfer-project by google.

the class CassandraCluster method createSession.

/**
 * Creates a session from the cluster configuration.
 *
 * @param ssl true if the session should use SSL.
 */
public Session createSession(boolean ssl) {
    try {
        Cluster.Builder builder = Cluster.builder();
        if (ssl) {
            SSLContext sslContext = SSLContext.getDefault();
            SSLOptions sslOptions = RemoteEndpointAwareJdkSSLOptions.builder().withSSLContext(sslContext).build();
            builder.withSSL(sslOptions);
        }
        cluster = builder.addContactPoint(host).withPort(port).withCredentials(username, password).build();
        return cluster.connect();
    } catch (Exception e) {
        throw new MicrosoftStorageException("Error creating connection to Cassandra", e);
    }
}
Also used : Cluster(com.datastax.driver.core.Cluster) SSLOptions(com.datastax.driver.core.SSLOptions) RemoteEndpointAwareJdkSSLOptions(com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions) SSLContext(javax.net.ssl.SSLContext)

Example 79 with Cluster

use of com.datastax.driver.core.Cluster in project ff4j by ff4j.

the class CassandraTest method testCassandraNoHost3.

/**
 * TDD.
 */
@Test(expected = IllegalArgumentException.class)
public void testCassandraNoHost3() {
    Cluster ccc = null;
    new CassandraConnection(ccc);
}
Also used : Cluster(com.datastax.driver.core.Cluster) Test(org.junit.Test)

Example 80 with Cluster

use of com.datastax.driver.core.Cluster in project ignite by apache.

the class CassandraSessionImplTest method executeFailureTest.

/**
 */
@SuppressWarnings("unchecked")
@Test
public void executeFailureTest() {
    Session session1 = mock(Session.class);
    Session session2 = mock(Session.class);
    when(session1.prepare(nullable(String.class))).thenReturn(preparedStatement1);
    when(session2.prepare(nullable(String.class))).thenReturn(preparedStatement2);
    ResultSetFuture rsFuture = mock(ResultSetFuture.class);
    ResultSet rs = mock(ResultSet.class);
    Iterator it = mock(Iterator.class);
    when(it.hasNext()).thenReturn(true);
    when(it.next()).thenReturn(mock(Row.class));
    when(rs.iterator()).thenReturn(it);
    when(rsFuture.getUninterruptibly()).thenReturn(rs);
    /* @formatter:off */
    when(session1.executeAsync(any(Statement.class))).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance")).thenThrow(new RuntimeException("this session should be refreshed / recreated"));
    when(session2.executeAsync(boundStatement1)).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"));
    when(session2.executeAsync(boundStatement2)).thenReturn(rsFuture);
    /* @formatter:on */
    Cluster cluster = mock(Cluster.class);
    when(cluster.connect()).thenReturn(session1).thenReturn(session2);
    when(session1.getCluster()).thenReturn(cluster);
    when(session2.getCluster()).thenReturn(cluster);
    Cluster.Builder builder = mock(Cluster.Builder.class);
    when(builder.build()).thenReturn(cluster);
    CassandraSessionImpl cassandraSession = new CassandraSessionImpl(builder, null, ConsistencyLevel.ONE, ConsistencyLevel.ONE, 0, mock(IgniteLogger.class));
    BatchExecutionAssistant<String, String> batchExecutionAssistant = new MyBatchExecutionAssistant();
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        data.add(String.valueOf(i));
    }
    cassandraSession.execute(batchExecutionAssistant, data);
    verify(cluster, times(2)).connect();
    verify(session1, times(1)).prepare(nullable(String.class));
    verify(session2, times(1)).prepare(nullable(String.class));
    assertEquals(10, batchExecutionAssistant.processedCount());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) CassandraSessionImpl(org.apache.ignite.cache.store.cassandra.session.CassandraSessionImpl) ResultSet(com.datastax.driver.core.ResultSet) Iterator(java.util.Iterator) Row(com.datastax.driver.core.Row) IgniteLogger(org.apache.ignite.IgniteLogger) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

Cluster (com.datastax.driver.core.Cluster)123 Session (com.datastax.driver.core.Session)74 ResultSet (com.datastax.driver.core.ResultSet)41 Row (com.datastax.driver.core.Row)37 Test (org.junit.Test)33 ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)30 Host (com.datastax.driver.core.Host)10 Metadata (com.datastax.driver.core.Metadata)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)9 ArrayList (java.util.ArrayList)9 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)8 IOException (java.io.IOException)8 PoolingOptions (com.datastax.driver.core.PoolingOptions)7 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)7 BeforeClass (org.junit.BeforeClass)7 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)6 QueryOptions (com.datastax.driver.core.QueryOptions)6 InetSocketAddress (java.net.InetSocketAddress)6 BoundStatement (com.datastax.driver.core.BoundStatement)5 SocketOptions (com.datastax.driver.core.SocketOptions)5