use of com.datastax.driver.core.Cluster in project camel by apache.
the class CassandraComponentProducerTest method testEndpointNoCqlParameter.
/**
* Simulate different CQL statements in the incoming message containing a header with RegularStatement, justifying the cassandracql endpoint not containing a "cql" Uri parameter
*/
@Test
public void testEndpointNoCqlParameter() throws Exception {
if (!canTest()) {
return;
}
Update.Where updateFirstName = update("camel_user").with(set("first_name", bindMarker())).where(eq("login", bindMarker()));
@SuppressWarnings("unused") Object response1 = producerTemplateNoEndpointCql.requestBodyAndHeader(new Object[] { "Claus 2", "c_ibsen" }, CassandraConstants.CQL_QUERY, updateFirstName);
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet1 = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row1 = resultSet1.one();
assertNotNull(row1);
assertEquals("Claus 2", row1.getString("first_name"));
assertEquals("Ibsen", row1.getString("last_name"));
Update.Where updateLastName = update("camel_user").with(set("last_name", bindMarker())).where(eq("login", bindMarker()));
@SuppressWarnings("unused") Object response2 = producerTemplateNoEndpointCql.requestBodyAndHeader(new Object[] { "Ibsen 2", "c_ibsen" }, CassandraConstants.CQL_QUERY, updateLastName);
ResultSet resultSet2 = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row2 = resultSet2.one();
assertNotNull(row2);
assertEquals("Claus 2", row2.getString("first_name"));
assertEquals("Ibsen 2", row2.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.Cluster in project camel by apache.
the class CassandraComponentProducerUnpreparedTest method testRequestUriCql.
@Test
public void testRequestUriCql() throws Exception {
Object response = producerTemplate.requestBody(Arrays.asList("w_jiang", "Willem", "Jiang"));
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "w_jiang");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Willem", row.getString("first_name"));
assertEquals("Jiang", row.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.Cluster in project camel by apache.
the class CassandraComponentProducerUnpreparedTest method testRequestMessageCql.
@Test
public void testRequestMessageCql() throws Exception {
Object response = producerTemplate.requestBodyAndHeader(new Object[] { "Claus 2", "Ibsen 2", "c_ibsen" }, CassandraConstants.CQL_QUERY, "update camel_user set first_name=?, last_name=? where login=?");
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Claus 2", row.getString("first_name"));
assertEquals("Ibsen 2", row.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.Cluster 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;
}
use of com.datastax.driver.core.Cluster in project janusgraph by JanusGraph.
the class CQLStoreManager method initializeCluster.
Cluster initializeCluster() throws PermanentBackendException {
final Configuration configuration = getStorageConfig();
final List<InetSocketAddress> contactPoints;
try {
contactPoints = Array.of(this.hostnames).map(hostName -> hostName.split(":")).map(array -> Tuple.of(array[0], array.length == 2 ? Integer.parseInt(array[1]) : this.port)).map(tuple -> new InetSocketAddress(tuple._1, tuple._2)).toJavaList();
} catch (SecurityException | ArrayIndexOutOfBoundsException | NumberFormatException e) {
throw new PermanentBackendException("Error initialising cluster contact points", e);
}
final Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints).withClusterName(configuration.get(CLUSTER_NAME));
if (configuration.get(PROTOCOL_VERSION) != 0) {
builder.withProtocolVersion(ProtocolVersion.fromInt(configuration.get(PROTOCOL_VERSION)));
}
if (configuration.has(AUTH_USERNAME) && configuration.has(AUTH_PASSWORD)) {
builder.withCredentials(configuration.get(AUTH_USERNAME), configuration.get(AUTH_PASSWORD));
}
if (configuration.has(LOCAL_DATACENTER)) {
builder.withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(configuration.get(LOCAL_DATACENTER)).build()));
}
if (configuration.get(SSL_ENABLED)) {
try {
final TrustManager[] trustManagers;
try (final FileInputStream keyStoreStream = new FileInputStream(configuration.get(SSL_TRUSTSTORE_LOCATION))) {
final KeyStore keystore = KeyStore.getInstance("jks");
keystore.load(keyStoreStream, configuration.get(SSL_TRUSTSTORE_PASSWORD).toCharArray());
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keystore);
trustManagers = trustManagerFactory.getTrustManagers();
}
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
final JdkSSLOptions sslOptions = JdkSSLOptions.builder().withSSLContext(sslContext).build();
builder.withSSL(sslOptions);
} catch (NoSuchAlgorithmException | CertificateException | IOException | KeyStoreException | KeyManagementException e) {
throw new PermanentBackendException("Error initialising SSL connection properties", e);
}
}
// Build the PoolingOptions based on the configurations
PoolingOptions poolingOptions = new PoolingOptions();
poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, configuration.get(LOCAL_MAX_REQUESTS_PER_CONNECTION)).setMaxRequestsPerConnection(HostDistance.REMOTE, configuration.get(REMOTE_MAX_REQUESTS_PER_CONNECTION));
poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, configuration.get(LOCAL_CORE_CONNECTIONS_PER_HOST), configuration.get(LOCAL_MAX_CONNECTIONS_PER_HOST)).setConnectionsPerHost(HostDistance.REMOTE, configuration.get(REMOTE_CORE_CONNECTIONS_PER_HOST), configuration.get(REMOTE_MAX_CONNECTIONS_PER_HOST));
return builder.withPoolingOptions(poolingOptions).build();
}
Aggregations