Java Code Examples for com.datastax.driver.core.Cluster#Builder

The following examples show how to use com.datastax.driver.core.Cluster#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraAuditRepositoryTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
private void ensureClusterCreation() throws InterruptedException {
    // Retry the connection until we either connect or timeout
    Cluster.Builder cassandraClusterBuilder = Cluster.builder();
    Cluster cluster =
            cassandraClusterBuilder.addContactPoint(CLUSTER_HOST).withClusterName(CLUSTER_NAME_TEST).withPort(CLUSTER_PORT)
                    .build();
    int retryCount = 0;

    while (retryCount < MAX_RETRIES) {
        try {
            Session cassSession = cluster.connect();
            if (cassSession.getState().getConnectedHosts().size() > 0) {
                cassSession.close();
                return;
            }
        } catch (Exception e) {
            Thread.sleep(1000);
        }
        retryCount++;
    }

    throw new SkipException("Unable to connect to embedded Cassandra after " + MAX_RETRIES + " seconds.");
}
 
Example 2
Source File: CqlCount.java    From cassandra-count with Apache License 2.0 6 votes vote down vote up
private void setup()
throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException,
              CertificateException, UnrecoverableKeyException  {
// Connect to Cassandra
Cluster.Builder clusterBuilder = Cluster.builder()
    .addContactPoint(host)
    .withPort(port)
    .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()));
if (null != username)
    clusterBuilder = clusterBuilder.withCredentials(username, password);
       if (null != truststorePath)
           clusterBuilder = clusterBuilder.withSSL(createSSLOptions());

cluster = clusterBuilder.build();
       if (null == cluster) {
           throw new IOException("Could not create cluster");
       }
session = cluster.connect();
   }
 
Example 3
Source File: CassandraSessionFactory.java    From jmeter-cassandra with Apache License 2.0 5 votes vote down vote up
public static synchronized Session createSession(String sessionKey, Set<InetAddress> host, String keyspace, String username, String password, LoadBalancingPolicy loadBalancingPolicy) {

    instance = getInstance();
    Session session = instance.sessions.get(sessionKey);
      if (session == null) {

          Cluster.Builder cb = Cluster.builder()
                  .addContactPoints(host)
                  .withReconnectionPolicy(new ConstantReconnectionPolicy(10000)) ;

          if (loadBalancingPolicy != null ) {
              cb = cb.withLoadBalancingPolicy(loadBalancingPolicy);
          }

          if ( username != null && ! username.isEmpty()) {
              cb = cb.withCredentials(username, password);
          }

          Cluster cluster = cb.build();


          if (keyspace != null && !keyspace.isEmpty())
        session = cluster.connect(keyspace);
      else
        session = cluster.connect();

        instance.sessions.put(sessionKey, session);
    }
    return session;
  }
 
Example 4
Source File: CassandraSessionProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Cluster createCluster(List<InetSocketAddress> contactPoints, SSLContext sslContext,
                              String username, String password, String compressionType,
                              Optional<Integer> readTimeoutMillisOptional, Optional<Integer> connectTimeoutMillisOptional) {
    Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(contactPoints);

    if (sslContext != null) {
        JdkSSLOptions sslOptions = JdkSSLOptions.builder()
                .withSSLContext(sslContext)
                .build();
        builder = builder.withSSL(sslOptions);
    }

    if (username != null && password != null) {
        builder = builder.withCredentials(username, password);
    }

    if(ProtocolOptions.Compression.SNAPPY.equals(compressionType)) {
        builder = builder.withCompression(ProtocolOptions.Compression.SNAPPY);
    } else if(ProtocolOptions.Compression.LZ4.equals(compressionType)) {
        builder = builder.withCompression(ProtocolOptions.Compression.LZ4);
    }

    SocketOptions socketOptions = new SocketOptions();
    readTimeoutMillisOptional.ifPresent(socketOptions::setReadTimeoutMillis);
    connectTimeoutMillisOptional.ifPresent(socketOptions::setConnectTimeoutMillis);

    builder.withSocketOptions(socketOptions);

    return builder.build();
}
 
Example 5
Source File: BatchPojoExample.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
		env.setParallelism(1);

		List<CustomCassandraAnnotatedPojo> customCassandraAnnotatedPojos = IntStream.range(0, 20)
				.mapToObj(x -> new CustomCassandraAnnotatedPojo(UUID.randomUUID().toString(), x, 0))
				.collect(Collectors.toList());

		DataSet<CustomCassandraAnnotatedPojo> dataSet = env.fromCollection(customCassandraAnnotatedPojos);

		ClusterBuilder clusterBuilder = new ClusterBuilder() {
			private static final long serialVersionUID = -1754532803757154795L;

			@Override
			protected Cluster buildCluster(Cluster.Builder builder) {
				return builder.addContactPoints("127.0.0.1").build();
			}
		};

		dataSet.output(new CassandraPojoOutputFormat<>(clusterBuilder, CustomCassandraAnnotatedPojo.class, () -> new Mapper.Option[]{Mapper.Option.saveNullFields(true)}));

		env.execute("Write");

		/*
		 *	This is for the purpose of showing an example of creating a DataSet using CassandraPojoInputFormat.
		 */
		DataSet<CustomCassandraAnnotatedPojo> inputDS = env
			.createInput(new CassandraPojoInputFormat<>(
				SELECT_QUERY,
				clusterBuilder,
				CustomCassandraAnnotatedPojo.class,
				() -> new Mapper.Option[]{Mapper.Option.consistencyLevel(ConsistencyLevel.ANY)}
			));

		inputDS.print();
	}
 
Example 6
Source File: Installer.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private Session createSession() {
    Cluster.Builder clusterBuilder = new Cluster.Builder();
    clusterBuilder.addContactPoints(cassandraNodes.toArray(new String[] {}));
    if (useSSL) {
        SSLOptions sslOptions = null;
        try {
            String[] defaultCipherSuites = {"TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"};
            sslOptions = JdkSSLOptions.builder().withSSLContext(SSLContext.getDefault())
                    .withCipherSuites(defaultCipherSuites).build();
            clusterBuilder.withSSL(sslOptions);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SSL support is required but is not available in the JVM.", e);
        }
    }

    clusterBuilder.withoutJMXReporting();

    Cluster cluster = clusterBuilder.build();
    cluster.init();
    Session createdSession = null;
    try {
        createdSession = cluster.connect("system");
        return createdSession;
    } finally {
        if (createdSession == null) {
            cluster.close();
        }
    }
}
 
Example 7
Source File: BatchPojoExample.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        List<CustomCassandraAnnotatedPojo> customCassandraAnnotatedPojos = IntStream.range(0, 20)
                .mapToObj(x -> new CustomCassandraAnnotatedPojo(UUID.randomUUID().toString(), x, 0))
                .collect(Collectors.toList());

        DataSet<CustomCassandraAnnotatedPojo> dataSet = env.fromCollection(customCassandraAnnotatedPojos);

        ClusterBuilder clusterBuilder = new ClusterBuilder() {
            private static final long serialVersionUID = -1754532803757154795L;

            @Override
            protected Cluster buildCluster(Cluster.Builder builder) {
                return builder.addContactPoints("127.0.0.1").build();
            }
        };

        dataSet.output(new CassandraPojoOutputFormat<>(clusterBuilder, CustomCassandraAnnotatedPojo.class, () -> new Mapper.Option[]{Mapper.Option.saveNullFields(true)}));

        env.execute("zhisheng");

        /*
         *	This is for the purpose of showing an example of creating a DataSet using CassandraPojoInputFormat.
         */
        DataSet<CustomCassandraAnnotatedPojo> inputDS = env
                .createInput(new CassandraPojoInputFormat<>(
                        SELECT_QUERY,
                        clusterBuilder,
                        CustomCassandraAnnotatedPojo.class,
                        () -> new Mapper.Option[]{Mapper.Option.consistencyLevel(ConsistencyLevel.ANY)}
                ));

        inputDS.print();
    }
 
Example 8
Source File: BatchPojoExample.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        List<CustomCassandraAnnotatedPojo> customCassandraAnnotatedPojos = IntStream.range(0, 20)
                .mapToObj(x -> new CustomCassandraAnnotatedPojo(UUID.randomUUID().toString(), x, 0))
                .collect(Collectors.toList());

        DataSet<CustomCassandraAnnotatedPojo> dataSet = env.fromCollection(customCassandraAnnotatedPojos);

        ClusterBuilder clusterBuilder = new ClusterBuilder() {
            private static final long serialVersionUID = -1754532803757154795L;

            @Override
            protected Cluster buildCluster(Cluster.Builder builder) {
                return builder.addContactPoints("127.0.0.1").build();
            }
        };

        dataSet.output(new CassandraPojoOutputFormat<>(clusterBuilder, CustomCassandraAnnotatedPojo.class, () -> new Mapper.Option[]{Mapper.Option.saveNullFields(true)}));

        env.execute("zhisheng");

        /*
         *	This is for the purpose of showing an example of creating a DataSet using CassandraPojoInputFormat.
         */
        DataSet<CustomCassandraAnnotatedPojo> inputDS = env
                .createInput(new CassandraPojoInputFormat<>(
                        SELECT_QUERY,
                        clusterBuilder,
                        CustomCassandraAnnotatedPojo.class,
                        () -> new Mapper.Option[]{Mapper.Option.consistencyLevel(ConsistencyLevel.ANY)}
                ));

        inputDS.print();
    }
 
Example 9
Source File: CassandraSink.java    From ingestion with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void start() {

  // Connect to Cassandra cluster
  Cluster.Builder clusterBuilder = Cluster.builder()
      .addContactPointsWithPorts(contactPoints);
  if (!Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password)) {
    clusterBuilder = clusterBuilder.withCredentials(username, password);
  }
  this.cluster = clusterBuilder.build();
  this.session = this.cluster.connect();

  // Initialize database if CQL script is provided
  executeCqlScript(session, initCql);

  tables = new ArrayList<CassandraTable>();
  for (final String tableString : tableStrings) {
    final String[] fields = tableString.split("\\.");
    if (fields.length != 2) {
      throw new IllegalArgumentException("Invalid format: " + tableString);
    }
    final String keyspace = fields[0];
    final String table = fields[1];
    final TableMetadata tableMetadata = getTableMetadata(session, keyspace, table);
    tables.add(new CassandraTable(session, tableMetadata, ConsistencyLevel.valueOf(consistency), bodyColumn, ignoreCase));
  }

  this.sinkCounter.start();
  super.start();
}
 
Example 10
Source File: ClusterCassandraConnectionFactory.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
private Cluster createCluster(Cassandra cassandra) {
	SocketOptions socketOptions = new SocketOptions();
	socketOptions.setConnectTimeoutMillis(30000);
	socketOptions.setReadTimeoutMillis(30000);
	int port = cassandra.getPort();
	int sslPort = cassandra.getSslPort();
	Cluster.Builder builder = Cluster.builder().addContactPoints(cassandra.getAddress())
			.withPort(isSslEnabled() && sslPort != -1 ? sslPort : port)
			.withSocketOptions(socketOptions);
	if (!isMetricsEnabled()) {
		builder.withoutMetrics();
	}
	if (!isJmxEnabled()) {
		builder.withoutJMXReporting();
	}
	String username = getUsername();
	String password = getPassword();
	if (username != null && password != null) {
		builder.withCredentials(username, password);
	}
	if (isSslEnabled()) {
		RemoteEndpointAwareJdkSSLOptions.Builder sslOptionsBuilder = RemoteEndpointAwareJdkSSLOptions.builder();
		if (getKeystore() != null || getTruststore() != null) {
			sslOptionsBuilder.withSSLContext(getSslContext());
		}
		List<String> cipherSuites = getCipherSuites();
		if (!cipherSuites.isEmpty()) {
			sslOptionsBuilder.withCipherSuites(cipherSuites.toArray(new String[0]));
		}
		builder.withSSL(sslOptionsBuilder.build());
	}
	List<TypeCodec<?>> typeCodecs = getTypeCodecs();
	if (!typeCodecs.isEmpty()) {
		builder.withCodecRegistry(new CodecRegistry().register(typeCodecs));
	}
	this.clusterBuilderCustomizers.forEach(customizer -> customizer.accept(builder));
	return builder.build();
}
 
Example 11
Source File: MapConfiguredCqlClientFactoryTest.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetClusterBuilder() throws Exception {
    final Map<Object, Object> configuration = new HashMap<Object,Object>();
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_CQL_HOSTS, HOSTS);
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_CLUSTER_NAME, CLUSTER_NAME);
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_READ_TIMEOUT, READ_TIMEOUT);
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_CONNECT_TIMEOUT, CONNECT_TIMEOUT);
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_LOCAL_DATA_CENTER_NAME, DATA_CENTER_NAME);
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_CONSISTENCY, DEFAULT_CONSISTENCY_LEVEL.name());
    configuration.put(MapConfiguredCqlClientFactory.TRIDENT_CASSANDRA_SERIAL_CONSISTENCY, DEFAULT_SERIAL_CONSISTENCY_LEVEL.name());

    final CqlClientFactory factory =
            new MapConfiguredCqlClientFactory(configuration);

    final Cluster.Builder clusterBuilder = factory.getClusterBuilder();
    Assert.assertEquals(CLUSTER_NAME, clusterBuilder.getClusterName());
    final InetSocketAddress first = clusterBuilder.getContactPoints().get(0);
    final InetSocketAddress second = clusterBuilder.getContactPoints().get(1);
    Assert.assertEquals("localhost", first.getHostName());
    Assert.assertEquals(9042, first.getPort());
    Assert.assertEquals("remotehost", second.getHostName());
    Assert.assertEquals(1234, second.getPort());
    Assert.assertEquals(Integer.parseInt(CONNECT_TIMEOUT), clusterBuilder.getConfiguration().getSocketOptions().getConnectTimeoutMillis());
    Assert.assertEquals(Integer.parseInt(READ_TIMEOUT), clusterBuilder.getConfiguration().getSocketOptions().getReadTimeoutMillis());
    Assert.assertEquals(DEFAULT_CONSISTENCY_LEVEL, clusterBuilder.getConfiguration().getQueryOptions().getConsistencyLevel());
    Assert.assertEquals(DEFAULT_SERIAL_CONSISTENCY_LEVEL, clusterBuilder.getConfiguration().getQueryOptions().getSerialConsistencyLevel());
    Assert.assertEquals(ProtocolOptions.Compression.NONE, clusterBuilder.getConfiguration().getProtocolOptions().getCompression());
}
 
Example 12
Source File: LiveCassandraManager.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
@Override
public Session createSession() {
    Cluster.Builder clusterBuilder = new Cluster.Builder()
            .withPort(9042)
            .withoutJMXReporting()
            .withPoolingOptions(new PoolingOptions()
                            .setMaxConnectionsPerHost(HostDistance.LOCAL, 1024)
                            .setCoreConnectionsPerHost(HostDistance.LOCAL, 1024)
                            .setMaxConnectionsPerHost(HostDistance.REMOTE, 1024)
                            .setCoreConnectionsPerHost(HostDistance.REMOTE, 1024)
                            .setMaxRequestsPerConnection(HostDistance.LOCAL, 1024)
                            .setMaxRequestsPerConnection(HostDistance.REMOTE, 1024)
                            .setMaxQueueSize(1024));

    Arrays.stream(nodes.split(",")).forEach(clusterBuilder::addContactPoints);

    cluster = clusterBuilder.build();
    cluster.init();
    try {
        session = cluster.connect("system");
        return session;
    } finally {
        if (session == null) {
            cluster.close();
        }
    }
}
 
Example 13
Source File: ClusterCassandraConnectionBuilder.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Add customizers that should be applied to the {@link Cluster.Builder}.
 *
 * @param clusterBuilderCustomizers the customizers to add
 * @return this builder
 */
@SafeVarargs
public final ClusterCassandraConnectionBuilder addClusterBuilderCustomizers(
		Consumer<? super Cluster.Builder>... clusterBuilderCustomizers) {
	Objects.requireNonNull(clusterBuilderCustomizers, "'clusterBuilderCustomizers' must not be null");
	this.clusterBuilderCustomizers.addAll(Arrays.asList(clusterBuilderCustomizers));
	return this;
}
 
Example 14
Source File: CassandraClusterInfo.java    From hdfs2cass with Apache License 2.0 4 votes vote down vote up
public void init(final String keyspace, final String columnFamily) {

    this.keyspace = keyspace;
    this.columnFamily = columnFamily;

    // connect to the cluster
    Cluster.Builder clusterBuilder = Cluster.builder();
    clusterBuilder.addContactPoints(host);
    if (port != -1) {
      clusterBuilder.withPort(port);
    }

    // ask for some metadata
    logger.info("getting cluster metadata for {}.{}", keyspace, columnFamily);
    final TableMetadata tableMetadata;
    try (final Cluster cluster = clusterBuilder.build()) {
      Metadata clusterMetadata = cluster.getMetadata();
      KeyspaceMetadata keyspaceMetadata = clusterMetadata.getKeyspace('"' + keyspace + '"');
      tableMetadata = keyspaceMetadata.getTable('"' + columnFamily + '"');
      cqlSchema = tableMetadata.asCQLQuery();
      partitionerClass = clusterMetadata.getPartitioner();
      Class.forName(partitionerClass);
      numClusterNodes = clusterMetadata.getAllHosts().size();
      columns = tableMetadata.getColumns();
    } catch (ClassNotFoundException cnfe) {
      throw new CrunchRuntimeException("No such partitioner: " + partitionerClass, cnfe);
    } catch (NullPointerException npe) {
      String msg = String.format("No such keyspace/table: %s/%s", keyspace, columnFamily);
      throw new CrunchRuntimeException(msg, npe);
    }

    // map the partition key columns
    final List<ColumnMetadata> partitionKeyColumns = tableMetadata.getPartitionKey();
    partitionKeyIndexes = new int[partitionKeyColumns.size()];
    for (int i = 0; i < partitionKeyColumns.size(); i++) {
      final String keyColName = partitionKeyColumns.get(i).getName();
      int j;
      for (j = 0; j < columns.size(); j++) {
        if (columns.get(j).getName().equals(keyColName)) {
          partitionKeyIndexes[i] = j;
          logger.info("partition key column {} index {}", keyColName, j);
          break;
        }
      }
      if (j == columns.size()) {
        throw new CrunchRuntimeException("no matching column for key " + keyColName);
      }
    }
  }
 
Example 15
Source File: CCMBridge.java    From cassandra-jdbc-wrapper with Apache License 2.0 4 votes vote down vote up
public static CCMCluster create(int nbNodes, Cluster.Builder builder) {
    if (nbNodes == 0) throw new IllegalArgumentException();

    return new CCMCluster(CCMBridge.create("test", nbNodes), builder, nbNodes);
}
 
Example 16
Source File: SimulacronDriverSupport.java    From simulacron with Apache License 2.0 4 votes vote down vote up
/** @return A default cluster builder using {@link #nonQuietClusterCloseOptions}. */
public static Cluster.Builder defaultBuilder() {
  return Cluster.builder().withNettyOptions(nonQuietClusterCloseOptions);
}
 
Example 17
Source File: CCMBridge.java    From cassandra-jdbc-wrapper with Apache License 2.0 4 votes vote down vote up
public static CCMBridge.CCMCluster buildCluster(int nbNodesDC1, int nbNodesDC2, Cluster.Builder builder) {
    return CCMCluster.create(nbNodesDC1, nbNodesDC2, builder);
}
 
Example 18
Source File: CCMBridge.java    From cassandra-jdbc-wrapper with Apache License 2.0 4 votes vote down vote up
public static CCMCluster create(int nbNodesDC1, int nbNodesDC2, Cluster.Builder builder) {
    if (nbNodesDC1 == 0) throw new IllegalArgumentException();

    return new CCMCluster(CCMBridge.create("test", nbNodesDC1, nbNodesDC2), builder, nbNodesDC1 + nbNodesDC2);
}
 
Example 19
Source File: SimulacronDriverSupport.java    From simulacron with Apache License 2.0 2 votes vote down vote up
/**
 * @param node to connect to as contact point.
 * @return a builder to connect to the input node.
 */
public static Cluster.Builder defaultBuilder(BoundNode node) {
  return defaultBuilder().withPort(node.port()).addContactPoints(node.inet());
}
 
Example 20
Source File: CqlClientFactory.java    From storm-cassandra-cql with Apache License 2.0 2 votes vote down vote up
/**
 * Subclasses must implement the creation of
 * of the Cluster.Builder.
 *
 * @return
 */
abstract Cluster.Builder getClusterBuilder();