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;
}
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();
}
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);
}
}
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);
}
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());
}
Aggregations