Search in sources :

Example 56 with Cluster

use of com.datastax.driver.core.Cluster in project stargate-core by tuplejump.

the class CQLDataLoaderD method createSession.

public Session createSession() {
    Cluster.Builder builder = new Cluster.Builder();
    Set<Map.Entry<String, Integer>> entries = hostsAndPorts.entrySet();
    for (Map.Entry<String, Integer> entry : entries) {
        builder.addContactPoints(entry.getKey()).withPort(entry.getValue());
    }
    Cluster cluster = builder.build();
    session = cluster.connect();
    return session;
}
Also used : Cluster(com.datastax.driver.core.Cluster) Map(java.util.Map)

Example 57 with Cluster

use of com.datastax.driver.core.Cluster in project metacat by Netflix.

the class CassandraConnectorFactory method stop.

/**
 * {@inheritDoc}
 */
@Override
public void stop() {
    super.stop();
    // Stop the cassandra cluster
    final Cluster cluster = this.getInjector().getInstance(Cluster.class);
    if (cluster != null) {
        cluster.close();
    }
}
Also used : Cluster(com.datastax.driver.core.Cluster)

Example 58 with Cluster

use of com.datastax.driver.core.Cluster in project apex-malhar by apache.

the class ConnectionStateManager method establishSessionWithPolicies.

private void establishSessionWithPolicies() {
    Cluster.Builder clusterBuilder = Cluster.builder();
    String[] seedNodesSplit = seedNodesStr.split(";");
    Collection<InetAddress> allSeeds = new ArrayList<>();
    Set<String> allKnownPorts = new HashSet<>();
    for (String seedNode : seedNodesSplit) {
        String[] nodeAndPort = seedNode.split(":");
        if (nodeAndPort.length > 1) {
            allKnownPorts.add(nodeAndPort[1]);
        }
        try {
            allSeeds.add(InetAddress.getByName(nodeAndPort[0]));
        } catch (UnknownHostException e) {
            LOG.error(" Error while trying to initialize the seed brokers for the cassandra cluster " + e.getMessage(), e);
        }
    }
    clusterBuilder = clusterBuilder.addContactPoints(allSeeds);
    clusterBuilder.withClusterName(clusterName).withLoadBalancingPolicy(loadBalancingPolicy).withRetryPolicy(retryPolicy).withQueryOptions(queryOptions).withReconnectionPolicy(reconnectionPolicy);
    // Finally initialize the cluster info
    if (allKnownPorts.size() > 0) {
        int shortlistedPort = Integer.parseInt(allKnownPorts.iterator().next());
        clusterBuilder = clusterBuilder.withPort(shortlistedPort);
    }
    cluster = clusterBuilder.build();
    Metadata metadata = cluster.getMetadata();
    LOG.info("Connected to cluster: \n" + metadata.getClusterName());
    for (Host host : metadata.getAllHosts()) {
        LOG.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
    }
    session = cluster.connect(keyspaceName);
}
Also used : UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet)

Example 59 with Cluster

use of com.datastax.driver.core.Cluster in project camel by apache.

the class CqlPopulateBean method populate.

public void populate() {
    Cluster cluster = Cluster.builder().addContactPoint("cassandra").build();
    Session session = cluster.connect();
    session.execute("create keyspace if not exists test with replication = {'class':'SimpleStrategy', 'replication_factor':1};");
    session.execute("create table if not exists test.users ( id int primary key, name text );");
    session.execute("insert into test.users (id,name) values (1, 'oscerd') if not exists;");
    session.close();
    cluster.close();
}
Also used : Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session)

Example 60 with Cluster

use of com.datastax.driver.core.Cluster in project zipkin by openzipkin.

the class Schema method getKeyspaceMetadata.

static KeyspaceMetadata getKeyspaceMetadata(Session session) {
    String keyspace = session.getLoggedKeyspace();
    Cluster cluster = session.getCluster();
    KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
    if (keyspaceMetadata == null) {
        throw new IllegalStateException(String.format("Cannot read keyspace metadata for give keyspace: %s and cluster: %s", keyspace, cluster.getClusterName()));
    }
    return keyspaceMetadata;
}
Also used : Cluster(com.datastax.driver.core.Cluster) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

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