本文整理了Java中com.datastax.driver.core.Cluster.connect()
方法的一些代码示例,展示了Cluster.connect()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cluster.connect()
方法的具体详情如下:
包路径:com.datastax.driver.core.Cluster
类名称:Cluster
方法名:connect
[英]Creates a new session on this cluster and initialize it.
Note that this method will initialize the newly created session, trying to connect to the Cassandra nodes before returning. If you only want to create a Session object without initializing it right away, see #newSession.
[中]在此群集上创建新会话并对其进行初始化。
请注意,此方法将初始化新创建的会话,在返回之前尝试连接到Cassandra节点。如果只想创建会话对象而不立即初始化它,请参阅#newSession。
代码示例来源:origin: kaaproject/kaa
/**
* Add field use_raw_configuration_schema to endpointProfile that used to support devices using
* SDK version 0.9.0
*/
public void transform() {
//mongo
MongoClient client = new MongoClient(host);
MongoDatabase database = client.getDatabase(dbName);
MongoCollection<Document> endpointProfile = database.getCollection("endpoint_profile");
endpointProfile.updateMany(new Document(), eq("$set", eq("use_raw_schema", false)));
//cassandra
Cluster cluster = Cluster.builder().addContactPoint(host).build();
Session session = cluster.connect(dbName);
session.execute("ALTER TABLE ep_profile ADD use_raw_schema boolean");
session.close();
cluster.close();
}
}
代码示例来源:origin: apache/ignite
/**
* Returns Cassandra session and its generation number.
*
* @return Wrapper object providing Cassandra session and its generation number.
*/
private synchronized WrappedSession session() {
if (wrapperSes != null)
return wrapperSes;
Session ses = SessionPool.get(this);
if (ses != null) {
this.wrapperSes = new WrappedSession(ses, generation);
return this.wrapperSes;
}
synchronized (sesStatements) {
sesStatements.clear();
}
try {
ses = builder.build().connect();
generation++;
this.wrapperSes = new WrappedSession(ses, generation);
}
catch (Throwable e) {
throw new IgniteException("Failed to establish session with Cassandra database", e);
}
return this.wrapperSes;
}
代码示例来源:origin: brianfrankcooper/YCSB
cluster = Cluster.builder().withCredentials(username, password)
.withPort(Integer.valueOf(port)).addContactPoints(hosts).build();
} else {
cluster = Cluster.builder().withPort(Integer.valueOf(port))
.addContactPoints(hosts).build();
session = cluster.connect(keyspace);
代码示例来源:origin: jooby-project/jooby
Cluster cluster = builder.build();
Session session = cluster.connect(cstr.keyspace());
hierarchy(session.getClass(), type -> bind.apply(type, cstr.keyspace(), session));
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@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)));
}
代码示例来源:origin: kaaproject/kaa
@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();
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short", expectedExceptions = AuthenticationException.class)
public void should_fail_to_connect_without_credentials() {
Cluster cluster =
register(
Cluster.builder()
.addContactPoints(getContactPoints())
.withPort(ccm().getBinaryPort())
.build());
cluster.connect();
}
代码示例来源:origin: pulsarIO/realtime-analytics
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());
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@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);
}
代码示例来源:origin: pulsarIO/realtime-analytics
private void connectInternal() {
try {
Cluster cluster = config.createBuilder().build();
cassandraSession = cluster.connect(keySpace);
cassandraMetrics = cluster.getMetrics();
connected.set(true);
} catch (Exception e) {
LOGGER.error("Error connection to Cassandra" + e.getMessage());
if (pool != null) {
pool.shutdownNow();
pool = null;
}
if (cassandraSession != null) {
cassandraSession.close();
if (cassandraSession.getCluster() != null)
cassandraSession.getCluster().close();
}
connected.set(false);
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
Cluster cluster =
register(
Cluster.builder()
.addContactPoints(getContactPoints())
.withPort(ccm().getBinaryPort())
.withCodecRegistry(codecRegistry)
.build());
Session session = cluster.connect(keyspace);
setUpTupleTypes(cluster);
codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType)));
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@BeforeMethod(groups = {"short", "unit"})
public void setUp() {
originalSlow = slow.getLevel();
originalError = error.getLevel();
slow.setLevel(INFO);
error.setLevel(INFO);
slow.addAppender(slowAppender = new MemoryAppender());
error.addAppender(errorAppender = new MemoryAppender());
queryLogger = null;
cluster = createClusterBuilder().withRetryPolicy(FallthroughRetryPolicy.INSTANCE).build();
session = cluster.connect();
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
Cluster cluster =
register(
Cluster.builder()
.addContactPoints(getContactPoints())
.withPort(ccm().getBinaryPort())
.withCredentials("bogus", "bogus")
.build());
cluster.connect();
fail("Should throw AuthenticationException when attempting to connect");
} catch (AuthenticationException e) {
try {
cluster.connect();
fail("Should throw IllegalStateException when attempting to connect again.");
} catch (IllegalStateException e1) {
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@BeforeClass(groups = {"short", "long"})
public void beforeTestClass() {
super.beforeTestClass();
Cluster.Builder builder = createClusterBuilder();
cluster = builder.build();
host = retrieveSingleHost(cluster);
session = cluster.connect();
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
Cluster cluster =
register(
Cluster.builder()
.addContactPoints(getContactPoints())
.withPort(ccm().getBinaryPort())
.withCodecRegistry(codecRegistry)
.build());
Session session = cluster.connect(keyspace);
setUpTupleTypes(cluster);
codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType)));
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
/**
* When no consistency level is defined the default of LOCAL_ONE should be used.
*
* @test_category consistency
*/
@Test(groups = "short")
public void should_use_global_default_cl_when_none_specified() throws Throwable {
// Build a cluster with no CL level set in the query options.
Cluster cluster = builder().build();
try {
Session session = cluster.connect();
// Construct unique simple statement query, with no CL defined.
// Check to ensure
String queryString = "default_cl";
Query clQuery = executeSimple(session, queryString, null, null);
assertTrue(clQuery.getConsistency().equals(ConsistencyLevel.LOCAL_ONE.toString()));
// Check prepared statement default CL
String prepareString = "prepared_default_cl";
PreparedStatementExecution pse = executePrepared(session, prepareString, null, null);
assertTrue(pse.getConsistency().equals(ConsistencyLevel.LOCAL_ONE.toString()));
// Check batch statement default CL
String batchStateString = "batch_default_cl";
BatchExecution batch = executeBatch(session, batchStateString, null, null);
assertTrue(batch.getConsistency().equals(ConsistencyLevel.LOCAL_ONE.toString()));
} finally {
cluster.close();
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
public void should_detect_cluster_init_failure_and_close_cluster() {
Cluster cluster =
Cluster.builder()
.addContactPointsWithPorts(new InetSocketAddress("127.0.0.1", 65534))
.withNettyOptions(nonQuietClusterCloseOptions)
.build();
try {
cluster.connect();
fail("Should not have been able to connect.");
} catch (NoHostAvailableException e) {
try {
cluster.connect();
fail("Should error when connect is called.");
} catch (IllegalStateException e1) {
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
Cluster cluster = builder().build();
try {
Session session = cluster.connect();
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@BeforeMethod(groups = "short")
public void setUp() {
sCluster = ScassandraCluster.builder().withNodes(4).build();
sCluster.init();
cluster =
Cluster.builder()
.addContactPoints(sCluster.address(1).getAddress())
.withPort(sCluster.getBinaryPort())
.withLoadBalancingPolicy(lbSpy)
.withNettyOptions(nonQuietClusterCloseOptions)
.build();
session = cluster.connect();
// Reset invocations before entering test.
Mockito.reset(lbSpy);
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
builder().withQueryOptions(new QueryOptions().setConsistencyLevel(cl)).build();
try {
Session session = cluster.connect();
内容来源于网络,如有侵权,请联系作者删除!