cluster = Cluster.builder().withCredentials(username, password) .withPort(Integer.valueOf(port)).addContactPoints(hosts).build(); } else { cluster = Cluster.builder().withPort(Integer.valueOf(port)) .addContactPoints(hosts).build();
.addContactPoints(cstr.contactPoints()) .withPort(cstr.port()); Cluster cluster = builder.build();
private void startUp() { int port = 9042; String[] seeds; if (configuration.containsKey(CONTACT_POINTS)) { seeds = configuration.get(CONTACT_POINTS).split(","); } else { seeds = new String[] {LOCALHOST}; } Cluster cluster = new Cluster.Builder() .addContactPoints(seeds) .withPort(port) .build(); String keySpace = configuration.get(KEY_SPACE); if (keySpace == null || keySpace.isEmpty()) { keySpace=DEFAULT_KEYSPACE; } session = Optional.of(cluster.connect(keySpace)); dataAccess = new DataAccess(session.get()); }
@Override protected void load() { String hostIp = EmbeddedCassandraServerHelper.getHost(); int port = EmbeddedCassandraServerHelper.getNativeTransportPort(); cluster = new Cluster.Builder().addContactPoints(hostIp).withPort(port).withSocketOptions(getSocketOptions()) .build(); session = cluster.connect(); CQLDataLoader dataLoader = new CQLDataLoader(session); dataLoader.load(dataSet); session = dataLoader.getSession(); }
protected void initTestCluster(Object testInstance) throws Exception { if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster()) { Cluster.Builder builder = ccmTestConfig.clusterProvider(testInstance); // add contact points only if the provided builder didn't do so if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints()); builder.withPort(ccm.getBinaryPort()); cluster = register(builder.build()); cluster.init(); } }
protected void resetTestSession() throws Exception { session.close(); Cluster.Builder builder = ccmTestConfig.clusterProvider(this); // add contact points only if the provided builder didn't do so if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints()); builder.withPort(ccm.getBinaryPort()); cluster = register(builder.build()); cluster.init(); session.close(); session = register(cluster.connect()); useKeyspace(session, keyspace); }
public void useNamedValuesWithProtocol(ProtocolVersion version) { Cluster vCluster = createClusterBuilder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withProtocolVersion(version) .build(); try { Session vSession = vCluster.connect(this.keyspace); // Given - A simple statement with named parameters. SimpleStatement statement = new SimpleStatement( "SELECT * FROM users WHERE id = :id", ImmutableMap.<String, Object>of("id", 1)); // When - Executing that statement against a Cluster instance using Protocol Version V2. vSession.execute(statement).one(); // Then - Should throw an UnsupportedFeatureException } finally { vCluster.close(); } }
Cluster cluster = builder() .addContactPoints(sCluster.address(1, 1).getAddress()) .withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy( DCAwareRoundRobinPolicy.builder().withLocalDc(datacenter(1)).build()) .build(); try { sCluster.init();
Cluster cluster = builder() .addContactPoints(sCluster.address(1, 1).getAddress()) .withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(policy) .build();
public TestHost(InetSocketAddress address) { super(address, new ConvictionPolicy.DefaultConvictionPolicy.Factory(), Cluster.builder().addContactPoints("localhost").build().manager); } }
.setMetadataEnabled(true); // choose whether to have the driver store metadata such as schema info Cluster.Builder datastaxCluster = Cluster.builder() .withClusterName(cassandraConfig.getClusterName()) .addContactPoints(cassandraConfig.getHosts().split(",")) .withMaxSchemaAgreementWaitSeconds(45) .withCompression(ProtocolOptions.Compression.LZ4) return datastaxCluster.build();
return ses; Cluster.Builder builder = Cluster.builder(); builder = builder.withPort(port); builder = builder.addContactPoints(contactPoints);
CassandraConf cassandraConf = new CassandraConf(topoConf); Cluster.Builder cluster = Cluster.builder() .withoutJMXReporting() .withoutMetrics() .addContactPoints(cassandraConf.getNodes()) .withPort(cassandraConf.getPort()) .withRetryPolicy(cassandraConf.getRetryPolicy()) .withReconnectionPolicy(new ExponentialReconnectionPolicy( cluster.withPoolingOptions(poolOps); return cluster.build();
/** * Tests the NoHostAvailableException. by attempting to build a cluster using the IP address * "255.255.255.255" and test all available exception methods. */ @Test(groups = "short") public void noHostAvailableException() throws Exception { try { Cluster.builder().addContactPoints("255.255.255.255").build(); } catch (NoHostAvailableException e) { assertEquals(e.getErrors().size(), 1); assertTrue( e.getErrors() .values() .iterator() .next() .toString() .contains("[/255.255.255.255] Cannot connect")); NoHostAvailableException copy = (NoHostAvailableException) e.copy(); assertEquals(copy.getMessage(), e.getMessage()); assertEquals(copy.getErrors(), e.getErrors()); } }
/** * Validates that when a Cluster is initialized that {@link * SpeculativeExecutionPolicy#init(Cluster)} is called and that when a Cluster is closed {@link * SpeculativeExecutionPolicy#close()} is called. * * @test_category queries:speculative_execution * @expected_result init and close are called on cluster init and close. * @jira_ticket JAVA-796 * @since 2.0.11, 2.1.7, 2.2.1 */ @Test(groups = "short") public void should_init_and_close_policy_on_cluster() { SpeculativeExecutionPolicy mockPolicy = mock(SpeculativeExecutionPolicy.class); Cluster cluster = Cluster.builder() .addContactPoints(scassandras.address(2).getAddress()) .withPort(scassandras.getBinaryPort()) .withSpeculativeExecutionPolicy(mockPolicy) .build(); verify(mockPolicy, times(0)).init(cluster); verify(mockPolicy, times(0)).close(); try { cluster.init(); verify(mockPolicy, times(1)).init(cluster); } finally { cluster.close(); verify(mockPolicy, times(1)).close(); } }
Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withCodecRegistry(codecRegistry) .build()); Session session = cluster.connect(keyspace); setUpTupleTypes(cluster);
@Test(groups = "short") public void testMissingRpcAddressAtStartup() throws Exception { deleteNode2RpcAddressFromNode1(); // Use only one contact point to make sure that the control connection is on node1 Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints().get(0)) .withPort(ccm().getBinaryPort()) .build()); cluster.connect(); // Since node2's RPC address is unknown on our control host, it should have been ignored assertEquals(cluster.getMetrics().getConnectedToHosts().getValue().intValue(), 1); assertNull(cluster.getMetadata().getHost(getContactPointsWithPorts().get(1))); }
Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withCodecRegistry(codecRegistry) .build()); Session session = cluster.connect(keyspace); setUpTupleTypes(cluster);
Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withCodecRegistry(codecRegistry) .build()); Session session = cluster.connect(keyspace); setUpUserTypes(cluster);
@Test(groups = "short") public void should_connect_with_credentials() { PlainTextAuthProvider authProvider = spy(new PlainTextAuthProvider("cassandra", "cassandra")); Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withAuthProvider(authProvider) .build(); cluster.connect(); verify(authProvider, atLeastOnce()) .newAuthenticator( findHost(cluster, 1).getSocketAddress(), "org.apache.cassandra.auth.PasswordAuthenticator"); assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()) .isEqualTo(0); }