本文整理汇总了Java中com.datastax.driver.core.Cluster.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java Cluster.Builder方法的具体用法?Java Cluster.Builder怎么用?Java Cluster.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.datastax.driver.core.Cluster
的用法示例。
在下文中一共展示了Cluster.Builder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCluster
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
/**
* Get a Cassandra cluster using hosts and port.
*/
private Cluster getCluster(List<String> hosts, int port, String username, String password,
String localDc, String consistencyLevel) {
Cluster.Builder builder = Cluster.builder()
.addContactPoints(hosts.toArray(new String[0]))
.withPort(port);
if (username != null) {
builder.withAuthProvider(new PlainTextAuthProvider(username, password));
}
if (localDc != null) {
builder.withLoadBalancingPolicy(
new TokenAwarePolicy(new DCAwareRoundRobinPolicy.Builder().withLocalDc(localDc).build()));
} else {
builder.withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
}
if (consistencyLevel != null) {
builder.withQueryOptions(
new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel)));
}
return builder.build();
}
示例2: getClientSession
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
public static Session getClientSession(String hostAddr) {
if(REGISTRY.containsKey(hostAddr)) {
return REGISTRY.get(hostAddr);
} else {
Cluster.Builder clientClusterBuilder = new Cluster.Builder()
.addContactPoint(hostAddr)
.withQueryOptions(new QueryOptions()
.setConsistencyLevel(ConsistencyLevel.ONE)
.setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL))
.withoutJMXReporting()
.withoutMetrics()
.withReconnectionPolicy(new ConstantReconnectionPolicy(RECONNECT_DELAY_IN_MS));
long startTimeInMillis = System.currentTimeMillis();
Cluster clientCluster = clientClusterBuilder.build();
Session clientSession = clientCluster.connect();
LOG.info("Client session established after {} ms.", System.currentTimeMillis() - startTimeInMillis);
REGISTRY.putIfAbsent(hostAddr, clientSession);
return clientSession;
}
}
示例3: adminSession
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
/** */
private static synchronized Session adminSession() {
if (adminSes != null) {
return adminSes;
}
try {
Cluster.Builder builder = Cluster.builder();
builder = builder.withCredentials(getAdminUser(), getAdminPassword());
builder.addContactPoints(getContactPoints());
builder.addContactPointsWithPorts(getContactPointsWithPorts());
adminCluster = builder.build();
return adminSes = adminCluster.connect();
}
catch (Throwable e) {
throw new RuntimeException("Failed to create admin session to Cassandra database", e);
}
}
示例4: regularSession
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
/** */
private static synchronized Session regularSession() {
if (regularSes != null) {
return regularSes;
}
try {
Cluster.Builder builder = Cluster.builder();
builder = builder.withCredentials(getRegularUser(), getRegularPassword());
builder.addContactPoints(getContactPoints());
builder.addContactPointsWithPorts(getContactPointsWithPorts());
regularCluster = builder.build();
return regularSes = regularCluster.connect();
}
catch (Throwable e) {
throw new RuntimeException("Failed to create regular session to Cassandra database", e);
}
}
示例5: setup
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
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();
}
示例6: init
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
public void init(Class<T> tClass) {
try {
Cluster.Builder builder = Cluster.builder();
final String[] nodesList = nodes.split(",");
for (String node : nodesList) {
builder.addContactPoint(node).withPort(Integer.parseInt(port));
LOGGER.info(String.format("Added cassandra node : %s", node + ":" + port));
}
cluster = builder.build();
session = null;
if (keyspace != null) {
session = cluster.connect(keyspace);
} else {
session = cluster.connect();
}
MappingManager mappingManager = new MappingManager(session);
mapper = mappingManager.mapper(tClass);
} catch (Exception e) {
LOGGER.error("Error initializing CassandraDao");
throw e;
}
}
示例7: buildCluster
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
static Cluster buildCluster(Cassandra3Storage cassandra) {
Cluster.Builder builder = Cluster.builder();
List<InetSocketAddress> contactPoints = parseContactPoints(cassandra);
int defaultPort = findConnectPort(contactPoints);
builder.addContactPointsWithPorts(contactPoints);
builder.withPort(defaultPort); // This ends up protocolOptions.port
if (cassandra.username != null && cassandra.password != null) {
builder.withCredentials(cassandra.username, cassandra.password);
}
builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE);
builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder(
cassandra.localDc != null
? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build()
: new RoundRobinPolicy()
// This can select remote, but LatencyAwarePolicy will prefer local
).build()));
builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(
HostDistance.LOCAL, cassandra.maxConnections
));
return builder.build();
}
示例8: buildCluster
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
static Cluster buildCluster(CassandraStorage cassandra) {
Cluster.Builder builder = Cluster.builder();
List<InetSocketAddress> contactPoints = parseContactPoints(cassandra);
int defaultPort = findConnectPort(contactPoints);
builder.addContactPointsWithPorts(contactPoints);
builder.withPort(defaultPort); // This ends up protocolOptions.port
if (cassandra.username != null && cassandra.password != null) {
builder.withCredentials(cassandra.username, cassandra.password);
}
builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE);
builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder(
cassandra.localDc != null
? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build()
: new RoundRobinPolicy()
// This can select remote, but LatencyAwarePolicy will prefer local
).build()));
builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(
HostDistance.LOCAL, cassandra.maxConnections
));
return builder.build();
}
示例9: getClusterBuilderConfigurer
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
@Override
protected ClusterBuilderConfigurer getClusterBuilderConfigurer() {
return new ClusterBuilderConfigurer() {
@Override
public Cluster.Builder configure(Cluster.Builder clusterBuilder) {
if (CassandraConfiguration.this.cassandraProperties.isUseSsl()) {
JdkSSLOptions.Builder optsBuilder = JdkSSLOptions.builder();
if (CassandraConfiguration.this.cassandraProperties.isSkipSslValidation()) {
try {
optsBuilder.withSSLContext(TrustAllSSLContextFactory.getSslContext());
}
catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Unable to configure a Cassandra cluster using SSL.", e);
}
}
return clusterBuilder.withSSL(optsBuilder.build());
}
else {
return clusterBuilder;
}
}
};
}
示例10: testHostNotFoundErrorHandling
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
@Test(expected = NoHostAvailableException.class)
public void testHostNotFoundErrorHandling() throws Exception {
CassandraSinkBase base = new CassandraSinkBase(new ClusterBuilder() {
@Override
protected Cluster buildCluster(Cluster.Builder builder) {
return builder
.addContactPoint("127.0.0.1")
.withoutJMXReporting()
.withoutMetrics().build();
}
}) {
@Override
public ListenableFuture send(Object value) {
return null;
}
};
base.open(new Configuration());
}
示例11: createClusterBuilder
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
protected Cluster.Builder createClusterBuilder() throws Exception {
CassandraLoadBalancingPolicies cassLoadBalancingPolicies = new CassandraLoadBalancingPolicies();
Cluster.Builder clusterBuilder = Cluster.builder();
for (String host : hosts.split(",")) {
clusterBuilder = clusterBuilder.addContactPoint(host);
}
if (port != null) {
clusterBuilder = clusterBuilder.withPort(port);
}
if (clusterName != null) {
clusterBuilder = clusterBuilder.withClusterName(clusterName);
}
if (username != null && !username.isEmpty() && password != null) {
clusterBuilder.withCredentials(username, password);
}
if (loadBalancingPolicy != null && !loadBalancingPolicy.isEmpty()) {
clusterBuilder.withLoadBalancingPolicy(cassLoadBalancingPolicies.getLoadBalancingPolicy(loadBalancingPolicy));
}
return clusterBuilder;
}
示例12: newSession
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
@Override
public CassandraSession newSession(CassandraSinkConnectorConfig config) {
Cluster.Builder clusterBuilder = Cluster.builder()
.withPort(config.port)
.addContactPoints(config.contactPoints)
.withProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED);
if (config.securityEnabled) {
clusterBuilder.withCredentials(config.username, config.password);
}
if (config.sslEnabled) {
final SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
sslContextBuilder.sslProvider(config.sslProvider);
final SslContext context;
try {
context = sslContextBuilder.build();
} catch (SSLException e) {
throw new ConnectException(e);
}
final SSLOptions sslOptions = new RemoteEndpointAwareNettySSLOptions(context);
clusterBuilder.withSSL(sslOptions);
}
clusterBuilder.withCompression(config.compression);
Cluster cluster = clusterBuilder.build();
log.info("Creating session");
final Session session = cluster.newSession();
return new CassandraSessionImpl(config, cluster, session);
}
示例13: provideCluster
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
/**
* Creates a cluster to use for connecting to Cassandra.
*
* @return The cluster singleton to use within the Injector
*/
@Provides
@Singleton
Cluster provideCluster() {
final Cluster.Builder builder = Cluster.builder().withClusterName(this.name);
// Contact points are required
final String contactPointsString = this.configuration.get(CONTACT_POINTS_KEY);
if (contactPointsString == null) {
throw new IllegalArgumentException(CONTACT_POINTS_KEY + " value is missing and is required.");
}
final String[] contactPoints = contactPointsString.split(",");
final ImmutableList.Builder<InetAddress> contactAddresses = ImmutableList.builder();
for (final String contactPoint : contactPoints) {
try {
contactAddresses.add(InetAddress.getByName(contactPoint));
} catch (final UnknownHostException uhe) {
throw new IllegalArgumentException("Can't parse contact point " + contactPoint, uhe);
}
}
builder.addContactPoints(contactAddresses.build());
final String port = this.configuration.get(PORT_KEY);
if (port != null) {
builder.withPort(Integer.parseInt(port));
}
final String username = this.configuration.get(USERNAME_KEY);
final String password = this.configuration.get(PASSWORD_KEY);
if (username != null && password != null) {
builder.withCredentials(username, password);
}
return builder.build();
}
示例14: dropTestKeyspace
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
public static void dropTestKeyspace( String keyspace, String[] hosts, int port ) {
Cluster.Builder builder = Cluster.builder();
for ( String host : hosts ) {
builder = builder.addContactPoint( host ).withPort( port );
}
final QueryOptions queryOptions = new QueryOptions().setConsistencyLevel( ConsistencyLevel.LOCAL_QUORUM );
builder.withQueryOptions( queryOptions );
Cluster cluster = builder.build();
Session session = cluster.connect();
logger.info("Dropping test keyspace: {}", keyspace);
session.execute( "DROP KEYSPACE IF EXISTS " + keyspace );
}
示例15: setHost
import com.datastax.driver.core.Cluster; //导入方法依赖的package包/类
/**
* Sets the cassandra host/port to connect to.
*
* @param host host to connect to
* @param port port to connect to
* @return this builder
*/
public CassandraSinkBuilder<IN> setHost(final String host, final int port) {
if (this.builder != null) {
throw new IllegalArgumentException("Builder was already set. You must use either setHost() or setClusterBuilder().");
}
this.builder = new ClusterBuilder() {
@Override
protected Cluster buildCluster(Cluster.Builder builder) {
return builder.addContactPoint(host).withPort(port).build();
}
};
return this;
}