本文整理汇总了Java中com.datastax.driver.core.policies.DCAwareRoundRobinPolicy类的典型用法代码示例。如果您正苦于以下问题:Java DCAwareRoundRobinPolicy类的具体用法?Java DCAwareRoundRobinPolicy怎么用?Java DCAwareRoundRobinPolicy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DCAwareRoundRobinPolicy类属于com.datastax.driver.core.policies包,在下文中一共展示了DCAwareRoundRobinPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCluster
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的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: startComponent
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
@Override
public void startComponent() {
if (cluster == null) {
// Configure and build up the Cassandra cluster.
cluster = Cluster.builder()
.withClusterName(clusterName)
.withPort(port)
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
// TokenAware requires query has routing info (e.g. BoundStatement with all PK value bound).
.withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()))
.addContactPoints(contactPoints.toArray(new String[contactPoints.size()]))
.build();
// Register any codecs.
cluster.getConfiguration().getCodecRegistry()
.register(new CassandraEnumCodec<>(AccessMode.class, AccessMode.getValueMap()))
.register(new CassandraEnumCodec<>(Direction.class, Direction.getValueMap()))
.register(new CassandraEnumCodec<>(SourceEntity.Type.class, SourceEntity.Type.getValueMap()));
// Create a session.
manager = new MappingManager(cluster.connect());
}
}
示例3: connect
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
static Session connect() {
String contactPoint = "localhost";
String keySpace = "ks1";
if(session == null) {
DCAwareRoundRobinPolicy dcAwarePolicy = new DCAwareRoundRobinPolicy.Builder().build();
LoadBalancingPolicy policy = new TokenAwarePolicy(dcAwarePolicy);
cluster = Cluster.builder().addContactPoint(contactPoint)
.withLoadBalancingPolicy(policy).build();
cluster.init();
for (Host host : cluster.getMetadata().getAllHosts()) {
System.out.printf("Address: %s, Rack: %s, Datacenter: %s, Tokens: %s\n", host.getAddress(),
host.getDatacenter(), host.getRack(), host.getTokens());
}
}
return session;
}
示例4: setup
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的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();
}
示例5: buildCluster
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的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();
}
示例6: buildCluster
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的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();
}
示例7: GenericRepair
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
public GenericRepair(String inputFile) throws IOException {
properties = new HashMap<String, String>();
loadProperties(inputFile);
cluster = Cluster.builder()
.addContactPoint(properties.get("contact_point"))
.withQueryOptions(new QueryOptions().setFetchSize(Integer.parseInt(properties.get("fetch_size"))))
.withCredentials(properties.get("username"), properties.get("password"))
.withSocketOptions(new SocketOptions().setConnectTimeoutMillis(1000000).setReadTimeoutMillis(1000000))
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(properties.get("local_dc"))
.build()).build();
session = cluster.connect();
scanConsistencyLevel = ConsistencyLevel.valueOf(properties.get("scan_consistency"));
fetchConsistencyLevel = ConsistencyLevel.ALL;
this.keyspace = properties.get("keyspace");
this.tableName = properties.get("table_name");
this.partitionKey = getPartitionKey();
this.individualFetchStatement = initializeIndividualFetchStatement();
this.partitionKeyColumnsToDataTypeName = new DataType.Name[partitionKey.split(",").length];
queue = new LinkedBlockingQueue<>(Integer.parseInt(properties.get("queue_size")));
sleepForFailedFetchStatement = Integer.parseInt(properties.get("sleep_millisconds"));
boundStatement = new BoundStatement(session.prepare("select distinct "+partitionKey+" from "+keyspace+"."+tableName));;
}
示例8: setup
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
private void setup()
throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException,
CertificateException, UnrecoverableKeyException {
// Connect to Cassandra
PoolingOptions pOpts = new PoolingOptions();
pOpts.setCoreConnectionsPerHost(HostDistance.LOCAL, 4);
pOpts.setMaxConnectionsPerHost(HostDistance.LOCAL, 4);
Cluster.Builder clusterBuilder = Cluster.builder()
.addContactPoint(host)
.withPort(port)
.withPoolingOptions(pOpts)
.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();
}
示例9: createCache
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
protected void createCache(Map<String, String> mapParams) throws Exception {
final Cluster.Builder bluePrint = Cluster.builder().withClusterName("BluePrint")
.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM))
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy()))
.addContactPoint(mapParams.get("cassandra.server.ip.address")).withPort(9042);
cache1 = mache(String.class, CassandraTestEntity.class)
.cachedBy(guava())
.storedIn(cassandra()
.withCluster(bluePrint)
.withKeyspace(mapParams.get("keyspace.name"))
.withSchemaOptions(SchemaOptions.CREATE_SCHEMA_IF_NEEDED)
.build())
.withMessaging(kafka()
.withKafkaMqConfig(KafkaMqConfigBuilder.builder()
.withZkHost(mapParams.get("kafka.connection"))
.build())
.withTopic(mapParams.get("kafka.topic"))
.build())
.macheUp();
}
示例10: build
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
@Override
public LoadBalancingPolicy build() {
DCAwareRoundRobinPolicy.Builder builder = DCAwareRoundRobinPolicy.builder();
if (allowRemoteDCsForLocalConsistencyLevel == Boolean.TRUE) {
builder.allowRemoteDCsForLocalConsistencyLevel();
}
if (localDC != null) {
builder.withLocalDc(localDC);
}
if (usedHostsPerRemoteDC != null) {
builder.withUsedHostsPerRemoteDc(usedHostsPerRemoteDC);
}
return builder.build();
}
示例11: main
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
public static void main(String[] args){
Cluster cluster;
Session session;
cluster = Cluster
.builder()
.addContactPoint("127.0.0.1")
.withRetryPolicy(DefaultRetryPolicy.INSTANCE) //Other option: DowngradingConsistencyRetryPolicy
.withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()))
.build();
session = cluster.connect("demo");
PreparedStatement statement = session.prepare("INSERT INTO user (id, name) VALUES (?, ?)");
Statement boundStatement = statement
.bind(1, "user 1")
.enableTracing();
long startTime = System.currentTimeMillis();
ResultSet resultSet = session.execute(boundStatement);
long duration = System.currentTimeMillis() - startTime;
System.out.format("Time taken: %d", duration);
ExecutionInfo executionInfo = resultSet.getExecutionInfo();
printQueryTrace(executionInfo.getQueryTrace());
cluster.close();
}
示例12: loadBalancing_settingLocalDcIgnoresOtherDatacenters
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
@Test
public void loadBalancing_settingLocalDcIgnoresOtherDatacenters() {
DCAwareRoundRobinPolicy policy = toDCAwareRoundRobinPolicy(
Cassandra3Storage.builder().localDc("bar").build());
Host foo = mock(Host.class);
when(foo.getDatacenter()).thenReturn("foo");
Host bar = mock(Host.class);
when(bar.getDatacenter()).thenReturn("bar");
policy.init(mock(Cluster.class), asList(foo, bar));
assertThat(policy.distance(foo)).isEqualTo(HostDistance.IGNORED);
assertThat(policy.distance(bar)).isEqualTo(HostDistance.LOCAL);
}
示例13: toDCAwareRoundRobinPolicy
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
DCAwareRoundRobinPolicy toDCAwareRoundRobinPolicy(Cassandra3Storage storage) {
return (DCAwareRoundRobinPolicy) ((LatencyAwarePolicy) ((TokenAwarePolicy) buildCluster(storage)
.getConfiguration()
.getPolicies()
.getLoadBalancingPolicy())
.getChildPolicy()).getChildPolicy();
}
示例14: loadBalancing_settingLocalDcIgnoresOtherDatacenters
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
@Test
public void loadBalancing_settingLocalDcIgnoresOtherDatacenters() {
DCAwareRoundRobinPolicy policy = toDCAwareRoundRobinPolicy(
CassandraStorage.builder().localDc("bar").build());
Host foo = mock(Host.class);
when(foo.getDatacenter()).thenReturn("foo");
Host bar = mock(Host.class);
when(bar.getDatacenter()).thenReturn("bar");
policy.init(mock(Cluster.class), asList(foo, bar));
assertThat(policy.distance(foo)).isEqualTo(HostDistance.IGNORED);
assertThat(policy.distance(bar)).isEqualTo(HostDistance.LOCAL);
}
示例15: toDCAwareRoundRobinPolicy
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; //导入依赖的package包/类
DCAwareRoundRobinPolicy toDCAwareRoundRobinPolicy(CassandraStorage storage) {
return (DCAwareRoundRobinPolicy) ((LatencyAwarePolicy) ((TokenAwarePolicy) buildCluster(storage)
.getConfiguration()
.getPolicies()
.getLoadBalancingPolicy())
.getChildPolicy()).getChildPolicy();
}