use of com.datastax.driver.core.Cluster in project nifi by apache.
the class AbstractCassandraProcessor method connectToCassandra.
protected void connectToCassandra(ProcessContext context) {
if (cluster.get() == null) {
ComponentLog log = getLogger();
final String contactPointList = context.getProperty(CONTACT_POINTS).evaluateAttributeExpressions().getValue();
final String consistencyLevel = context.getProperty(CONSISTENCY_LEVEL).getValue();
List<InetSocketAddress> contactPoints = getContactPoints(contactPointList);
// Set up the client for secure (SSL/TLS communications) if configured to do so
final SSLContextService sslService = context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
final SSLContext sslContext;
if (sslService != null) {
final SSLContextService.ClientAuth clientAuth;
if (StringUtils.isBlank(rawClientAuth)) {
clientAuth = SSLContextService.ClientAuth.REQUIRED;
} else {
try {
clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
} catch (final IllegalArgumentException iae) {
throw new ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
}
}
sslContext = sslService.createSSLContext(clientAuth);
} else {
sslContext = null;
}
final String username, password;
PropertyValue usernameProperty = context.getProperty(USERNAME).evaluateAttributeExpressions();
PropertyValue passwordProperty = context.getProperty(PASSWORD).evaluateAttributeExpressions();
if (usernameProperty != null && passwordProperty != null) {
username = usernameProperty.getValue();
password = passwordProperty.getValue();
} else {
username = null;
password = null;
}
// Create the cluster and connect to it
Cluster newCluster = createCluster(contactPoints, sslContext, username, password);
PropertyValue keyspaceProperty = context.getProperty(KEYSPACE).evaluateAttributeExpressions();
final Session newSession;
if (keyspaceProperty != null) {
newSession = newCluster.connect(keyspaceProperty.getValue());
} else {
newSession = newCluster.connect();
}
newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel));
Metadata metadata = newCluster.getMetadata();
log.info("Connected to Cassandra cluster: {}", new Object[] { metadata.getClusterName() });
cluster.set(newCluster);
cassandraSession.set(newSession);
}
}
use of com.datastax.driver.core.Cluster in project smscgateway by RestComm.
the class PersistenceRAInterfaceProxy method testCassandraAccess.
public boolean testCassandraAccess() {
try {
Cluster cluster = Cluster.builder().addContactPoint(ip).build();
try {
Metadata metadata = cluster.getMetadata();
for (Host host : metadata.getAllHosts()) {
logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
}
Session session = cluster.connect();
session.execute("USE \"" + keyspace + "\"");
// testing if a keyspace is acceptable
int tstRes = 0;
PreparedStatement ps;
BoundStatement boundStatement;
try {
ps = session.prepare("SELECT * from \"TEST_TABLE\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
session.execute(boundStatement);
tstRes = 1;
} catch (Exception e) {
int g1 = 0;
g1++;
}
ProtocolVersion protVersion = DBOperations.getProtocolVersion(cluster);
if (protVersion == ProtocolVersion.V1) {
throw new Exception("We do not support cassandra databse 1.2 more");
// if (tstRes == 0) {
// session.execute("CREATE TABLE \"TEST_TABLE\" (id uuid primary key);");
// }
//
// // deleting of current tables
// try {
// session.execute("TRUNCATE \"" + Schema.FAMILY_CURRENT_SLOT_TABLE + "\";");
// } catch (Exception e) {
// int g1 = 0;
// g1++;
// }
} else {
if (tstRes == 0) {
ps = session.prepare("CREATE TABLE \"TEST_TABLE\" (id uuid primary key);");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
session.execute(boundStatement);
}
// deleting of current tables
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_CURRENT_SLOT_TABLE + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
}
// 1
Date dt = new Date();
Date dt2 = new Date(new Date().getTime() + 1000 * 60 * 60 * 24);
Date dt3 = new Date(new Date().getTime() - 1000 * 60 * 60 * 24);
String tName = this.getTableName(dt);
doTrauncateTables(session, tName);
// 2
tName = this.getTableName(dt2);
doTrauncateTables(session, tName);
// 3
tName = this.getTableName(dt3);
doTrauncateTables(session, tName);
return true;
} finally {
cluster.close();
// cluster.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of com.datastax.driver.core.Cluster in project smscgateway by RestComm.
the class SmscDatabaseManagementTest method testCassandraAccess.
public boolean testCassandraAccess() {
try {
Cluster cluster = Cluster.builder().addContactPoint(ip).build();
try {
Metadata metadata = cluster.getMetadata();
Session session = cluster.connect();
return true;
} finally {
cluster.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of com.datastax.driver.core.Cluster in project smscgateway by RestComm.
the class TT_PersistenceProxy method testCassandraAccess.
public boolean testCassandraAccess() {
String ip = "127.0.0.1";
String keyspace = "RestCommSMSC";
try {
Cluster cluster = Cluster.builder().addContactPoint(ip).build();
try {
Metadata metadata = cluster.getMetadata();
for (Host host : metadata.getAllHosts()) {
logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
}
Session session = cluster.connect();
session.execute("USE \"" + keyspace + "\"");
// testing if a keyspace is acceptable
PreparedStatement ps = session.prepare("DROP TABLE \"TEST_TABLE\";");
BoundStatement boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
ps = session.prepare("CREATE TABLE \"TEST_TABLE\" ( id uuid primary key ) ;");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
session.execute(boundStatement);
// deleting of current tables
ps = session.prepare("DROP TABLE \"" + Schema.FAMILY_SMPP_SMS_ROUTING_RULE + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
return true;
} finally {
cluster.close();
// cluster.shutdown();
}
} catch (Exception e) {
return false;
}
// try {
//
// Cluster cluster = Cluster.builder().addContactPoint(ip).build();
// Metadata metadata = cluster.getMetadata();
//
// for (Host host : metadata.getAllHosts()) {
// logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
// }
//
// Session session = cluster.connect();
//
// session.execute("USE \"" + keyspace + "\"");
//
// PreparedStatement ps = session.prepare("select * from \"" + Schema.FAMILY_SMS_ROUTING_RULE + "\" limit 1;");
// BoundStatement boundStatement = new BoundStatement(ps);
// boundStatement.bind();
// session.execute(boundStatement);
//
// return true;
// } catch (Exception e) {
// return false;
// }
}
use of com.datastax.driver.core.Cluster in project jhipster-sample-app-cassandra by jhipster.
the class AbstractCassandraTest method startServer.
@BeforeClass
public static void startServer() throws TTransportException, ConfigurationException, IOException, URISyntaxException {
if (!started) {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_UNIT_RANDOM_PORT_YAML, CASSANDRA_TIMEOUT);
Cluster cluster = new Cluster.Builder().addContactPoints("127.0.0.1").withPort(getNativeTransportPort()).build();
Session session = cluster.connect();
String createQuery = "CREATE KEYSPACE " + CASSANDRA_UNIT_KEYSPACE + " WITH replication={'class' : 'SimpleStrategy', 'replication_factor':1}";
session.execute(createQuery);
String useKeyspaceQuery = "USE " + CASSANDRA_UNIT_KEYSPACE;
session.execute(useKeyspaceQuery);
CQLDataLoader dataLoader = new CQLDataLoader(session);
applyScripts(dataLoader, "config/cql/changelog/", "*.cql");
started = true;
}
}
Aggregations