Search in sources :

Example 11 with Metadata

use of com.datastax.driver.core.Metadata in project YCSB by brianfrankcooper.

the class CassandraCQLClient method init.

/**
   * Initialize any state for this DB. Called once per DB instance; there is one
   * DB instance per client thread.
   */
@Override
public void init() throws DBException {
    // Keep track of number of calls to init (for later cleanup)
    INIT_COUNT.incrementAndGet();
    // cluster/session instance for all the threads.
    synchronized (INIT_COUNT) {
        // Check if the cluster has already been initialized
        if (cluster != null) {
            return;
        }
        try {
            debug = Boolean.parseBoolean(getProperties().getProperty("debug", "false"));
            trace = Boolean.valueOf(getProperties().getProperty(TRACING_PROPERTY, TRACING_PROPERTY_DEFAULT));
            String host = getProperties().getProperty(HOSTS_PROPERTY);
            if (host == null) {
                throw new DBException(String.format("Required property \"%s\" missing for CassandraCQLClient", HOSTS_PROPERTY));
            }
            String[] hosts = host.split(",");
            String port = getProperties().getProperty(PORT_PROPERTY, PORT_PROPERTY_DEFAULT);
            String username = getProperties().getProperty(USERNAME_PROPERTY);
            String password = getProperties().getProperty(PASSWORD_PROPERTY);
            String keyspace = getProperties().getProperty(KEYSPACE_PROPERTY, KEYSPACE_PROPERTY_DEFAULT);
            readConsistencyLevel = ConsistencyLevel.valueOf(getProperties().getProperty(READ_CONSISTENCY_LEVEL_PROPERTY, READ_CONSISTENCY_LEVEL_PROPERTY_DEFAULT));
            writeConsistencyLevel = ConsistencyLevel.valueOf(getProperties().getProperty(WRITE_CONSISTENCY_LEVEL_PROPERTY, WRITE_CONSISTENCY_LEVEL_PROPERTY_DEFAULT));
            if ((username != null) && !username.isEmpty()) {
                cluster = Cluster.builder().withCredentials(username, password).withPort(Integer.valueOf(port)).addContactPoints(hosts).build();
            } else {
                cluster = Cluster.builder().withPort(Integer.valueOf(port)).addContactPoints(hosts).build();
            }
            String maxConnections = getProperties().getProperty(MAX_CONNECTIONS_PROPERTY);
            if (maxConnections != null) {
                cluster.getConfiguration().getPoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(maxConnections));
            }
            String coreConnections = getProperties().getProperty(CORE_CONNECTIONS_PROPERTY);
            if (coreConnections != null) {
                cluster.getConfiguration().getPoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(coreConnections));
            }
            String connectTimoutMillis = getProperties().getProperty(CONNECT_TIMEOUT_MILLIS_PROPERTY);
            if (connectTimoutMillis != null) {
                cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(Integer.valueOf(connectTimoutMillis));
            }
            String readTimoutMillis = getProperties().getProperty(READ_TIMEOUT_MILLIS_PROPERTY);
            if (readTimoutMillis != null) {
                cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(Integer.valueOf(readTimoutMillis));
            }
            Metadata metadata = cluster.getMetadata();
            System.err.printf("Connected to cluster: %s\n", metadata.getClusterName());
            for (Host discoveredHost : metadata.getAllHosts()) {
                System.out.printf("Datacenter: %s; Host: %s; Rack: %s\n", discoveredHost.getDatacenter(), discoveredHost.getAddress(), discoveredHost.getRack());
            }
            session = cluster.connect(keyspace);
        } catch (Exception e) {
            throw new DBException(e);
        }
    }
// synchronized
}
Also used : DBException(com.yahoo.ycsb.DBException) Metadata(com.datastax.driver.core.Metadata) Host(com.datastax.driver.core.Host) DBException(com.yahoo.ycsb.DBException)

Example 12 with Metadata

use of com.datastax.driver.core.Metadata 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 13 with Metadata

use of com.datastax.driver.core.Metadata 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 14 with Metadata

use of com.datastax.driver.core.Metadata 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 15 with Metadata

use of com.datastax.driver.core.Metadata 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)

Aggregations

Metadata (com.datastax.driver.core.Metadata)20 Host (com.datastax.driver.core.Host)13 Cluster (com.datastax.driver.core.Cluster)9 Session (com.datastax.driver.core.Session)6 Builder (com.datastax.driver.core.Cluster.Builder)4 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)4 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 TableMetadata (com.datastax.driver.core.TableMetadata)3 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)3 ProtocolVersion (com.datastax.driver.core.ProtocolVersion)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Date (java.util.Date)2 SSLContext (javax.net.ssl.SSLContext)2 XMLStreamException (javolution.xml.stream.XMLStreamException)2 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)2 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 QueryOptions (com.datastax.driver.core.QueryOptions)1 SSLOptions (com.datastax.driver.core.SSLOptions)1