Search in sources :

Example 71 with Cluster

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);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Metadata(com.datastax.driver.core.Metadata) PropertyValue(org.apache.nifi.components.PropertyValue) Cluster(com.datastax.driver.core.Cluster) SSLContext(javax.net.ssl.SSLContext) ComponentLog(org.apache.nifi.logging.ComponentLog) ProviderCreationException(org.apache.nifi.authentication.exception.ProviderCreationException) SSLContextService(org.apache.nifi.ssl.SSLContextService) Session(com.datastax.driver.core.Session)

Example 72 with Cluster

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;
    }
}
Also used : Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) PreparedStatement(com.datastax.driver.core.PreparedStatement) ProtocolVersion(com.datastax.driver.core.ProtocolVersion) BoundStatement(com.datastax.driver.core.BoundStatement) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) IOException(java.io.IOException) Date(java.util.Date) Session(com.datastax.driver.core.Session)

Example 73 with Cluster

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;
    }
}
Also used : Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session)

Example 74 with Cluster

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;
// }
}
Also used : Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session)

Example 75 with Cluster

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;
    }
}
Also used : Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session) CQLDataLoader(org.cassandraunit.CQLDataLoader) BeforeClass(org.junit.BeforeClass)

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