本文整理汇总了Java中com.datastax.driver.core.Host类的典型用法代码示例。如果您正苦于以下问题:Java Host类的具体用法?Java Host怎么用?Java Host使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Host类属于com.datastax.driver.core包,在下文中一共展示了Host类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
import com.datastax.driver.core.Host; //导入依赖的package包/类
private void connect(String seeds) {
if (getWithSSL()) {
LOGGER.info("SSL mode enabled");
try {
SSLOptions sslOptions = new SSLOptions(SSLContext.getDefault(), CIPHERS);
builder = Cluster.builder().withSSL(sslOptions);
} catch (NoSuchAlgorithmException e) {
LOGGER.error("Unable to setup SSL Options for Cassandra");
}
}
String[] contactPoints = seeds.split(",");
for (String contactPoint : contactPoints) {
LOGGER.info("Adding Cassandra contact point " + contactPoint);
builder.addContactPoints(contactPoint);
}
cluster = builder.build();
Metadata metadata = cluster.getMetadata();
for (Host host : metadata.getAllHosts()) {
LOGGER.info("Datacenter "+ host.getDatacenter() + "Host " + host.getAddress() + "Rack " + host.getRack());
session = cluster.connect();
}
}
开发者ID:emc-cloudfoundry,项目名称:cassandra-cf-service-boshrelease,代码行数:27,代码来源:CassandraAdminService.java
示例2: testVnodeSupport
import com.datastax.driver.core.Host; //导入依赖的package包/类
@Test
public void testVnodeSupport() throws Exception {
// Validate that peers as appropriately discovered when connecting to a node and vnodes are
// assigned.
try (BoundCluster boundCluster =
server.register(ClusterSpec.builder().withNumberOfTokens(256).withNodes(3, 3, 3));
Cluster driverCluster = defaultBuilder(boundCluster).build()) {
driverCluster.init();
// Should be 9 hosts
assertThat(driverCluster.getMetadata().getAllHosts()).hasSize(9);
Set<Token> allTokens = new HashSet<>();
for (Host host : driverCluster.getMetadata().getAllHosts()) {
assertThat(host.getTokens()).hasSize(256);
allTokens.addAll(host.getTokens());
}
// Should be 256*9 unique tokens.
assertThat(allTokens).hasSize(256 * 9);
}
}
示例3: execute
import com.datastax.driver.core.Host; //导入依赖的package包/类
@Override
public ResultSet<CassandraDBContext> execute(Query<CassandraDBContext> query) throws QueryExecutionException {
try (Cluster cassandraConnection = buildConnection()) {
final Metadata metadata = cassandraConnection.getMetadata();
System.out.printf("Connected to cluster: %s", metadata.getClusterName());
for (final Host host : metadata.getAllHosts()) {
System.out.printf("Datacenter: %s; Host: %s; Rack: %s", host.getDatacenter(), host.getAddress(),
host.getRack());
}
try (Session session = cassandraConnection.connect()) {
String queryToExecute = query.getQuery();
System.out.println(queryToExecute);
com.datastax.driver.core.ResultSet resultSet = session.execute(queryToExecute);
printResultSet(resultSet);
ExecutionInfo executionInfo = resultSet.getExecutionInfo();
System.out.println(executionInfo);
}
}
// There isn't any resultset for these use-case
return new CassandraResultSet();
}
示例4: connect
import com.datastax.driver.core.Host; //导入依赖的package包/类
static Session connect() {
String contactPoint = "localhost";
String keySpace = "ks1";
if(session == null) {
PoolingOptions poolingOptions = new PoolingOptions().setConnectionsPerHost(HostDistance.REMOTE, 1, 4);
cluster = Cluster.builder().addContactPoint(contactPoint).withPoolingOptions(poolingOptions)
.withCompression(Compression.SNAPPY).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());
}
session = cluster.connect(keySpace);
}
return session;
}
示例5: connect
import com.datastax.driver.core.Host; //导入依赖的package包/类
static Session connect() {
String contactPoint = "localhost";
String keySpace = "ks1";
if(session == null) {
RetryPolicy retryPolicy = new CustomRetryPolicy(3, 3, 2);
cluster = Cluster.builder().addContactPoint(contactPoint)
.withRetryPolicy(retryPolicy).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;
}
示例6: connect
import com.datastax.driver.core.Host; //导入依赖的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;
}
示例7: update
import com.datastax.driver.core.Host; //导入依赖的package包/类
@Override public void update(Host host, Statement statement, Exception e, long nanos) {
if (!(statement instanceof NamedBoundStatement)) return;
Span span = cache.remove(statement);
if (span == null) {
if (statement.isTracing()) {
LOG.warn("{} not in the cache eventhough tracing is on", statement);
}
return;
}
span.setDuration(nanos / 1000); // TODO: allow client tracer to end with duration
Endpoint local = span.getAnnotations().get(0).host; // TODO: expose in brave
long endTs = span.getTimestamp() + span.getDuration();
if (e != null) {
span.addToBinary_annotations(BinaryAnnotation.create("cql.error", e.getMessage(), local));
} else {
span.addToAnnotations(Annotation.create(endTs, "cr", local));
}
int ipv4 = ByteBuffer.wrap(host.getAddress().getAddress()).getInt();
Endpoint endpoint = Endpoint.create("cassandra", ipv4, host.getSocketAddress().getPort());
span.addToBinary_annotations(BinaryAnnotation.address("sa", endpoint));
collector.collect(span);
}
示例8: update
import com.datastax.driver.core.Host; //导入依赖的package包/类
@Override public void update(Host host, Statement statement, Exception e, long nanos) {
if (!(statement instanceof BoundStatement)) return;
Span span = cache.remove(statement);
if (span == null) {
if (statement.isTracing()) {
LOG.warn("{} not in the cache eventhough tracing is on", statement);
}
return;
}
span.setDuration(nanos / 1000); // TODO: allow client tracer to end with duration
Endpoint local = span.getAnnotations().get(0).host; // TODO: expose in brave
long endTs = span.getTimestamp() + span.getDuration();
span.addToAnnotations(Annotation.create(endTs, "cr", local));
if (e != null) {
span.addToBinary_annotations(BinaryAnnotation.create(Constants.ERROR, e.getMessage(), local));
}
int ipv4 = ByteBuffer.wrap(host.getAddress().getAddress()).getInt();
Endpoint endpoint = Endpoint.create("cassandra3", ipv4, host.getSocketAddress().getPort());
span.addToBinary_annotations(BinaryAnnotation.address("sa", endpoint));
collector.collect(span);
}
示例9: testClusterHintsPollerWhenNodeDown
import com.datastax.driver.core.Host; //导入依赖的package包/类
@Test
public void testClusterHintsPollerWhenNodeDown() throws UnknownHostException {
ClusterHintsPoller clusterHintsPoller = new ClusterHintsPoller();
Session mockSession = mock(Session.class);
Cluster mockCluster = mock(Cluster.class);
Metadata mockMetadata = mock(Metadata.class);
when(mockCluster.getMetadata()).thenReturn(mockMetadata);
when(mockCluster.getClusterName()).thenReturn("test-cluster");
Host node1 = mock(Host.class);
when(node1.getAddress()).thenReturn(InetAddress.getByName("127.0.0.1"));
Host node2 = mock(Host.class);
when(node2.getAddress()).thenReturn(InetAddress.getByName("127.0.0.2"));
Host node3 = mock(Host.class);
when(node3.getAddress()).thenReturn(InetAddress.getByName("127.0.0.3"));
when(mockSession.getCluster()).thenReturn(mockCluster);
// The first node queried is down
when(mockSession.execute(any(Statement.class))).thenThrow(new NoHostAvailableException(ImmutableMap.<InetSocketAddress, Throwable>of()));
when(mockMetadata.getAllHosts()).thenReturn(ImmutableSet.of(node1, node2, node3));
HintsPollerResult actualResult = clusterHintsPoller.getOldestHintsInfo(mockSession);
// Make sure HintsPollerResult fails
assertFalse(actualResult.areAllHostsPolling(), "Result should show hosts failing");
assertEquals(actualResult.getHostFailure(), ImmutableSet.of(InetAddress.getByName("127.0.0.1")), "Node 1 should return with host failure");
}
示例10: getHostStatementMatcher
import com.datastax.driver.core.Host; //导入依赖的package包/类
private ArgumentMatcher<Statement> getHostStatementMatcher(final Host host, final String query)
throws Exception {
return new ArgumentMatcher<Statement>() {
@Override
public boolean matches(Object argument) {
SelectedHostStatement statement = (SelectedHostStatement) argument;
return ((SimpleStatement)statement.getStatement()).getQueryString().equals(query) &&
Objects.equals(statement.getHostCordinator().getAddress(), host.getAddress());
}
@Override
public void describeTo(Description description) {
description.appendText(format("query:%s host:%s", query, host.getAddress().toString()));
}
};
}
示例11: pingAll
import com.datastax.driver.core.Host; //导入依赖的package包/类
private Result pingAll() {
try {
StringBuilder message = new StringBuilder();
OperationResult<CqlStatementResult> astyanaxResult = pingAstyanax();
message.append("Astyanax: ").append(astyanaxResult.getHost()).append(" ")
.append(astyanaxResult.getLatency(TimeUnit.MICROSECONDS)).append("us");
if (astyanaxResult.getAttemptsCount() != 1) {
message.append(", ").append(astyanaxResult.getAttemptsCount()).append(" attempts");
}
Stopwatch cqlTimer = Stopwatch.createStarted();
ResultSet cqlResult = pingCql();
long queryDurationMicros = cqlTimer.elapsed(TimeUnit.MICROSECONDS);
Host host = cqlResult.getExecutionInfo().getQueriedHost();
message.append(" | CQL: ").append(host).append(" ").append(queryDurationMicros).append("us");
return Result.healthy(message.toString());
} catch (Throwable t) {
return Result.unhealthy(t);
}
}
示例12: sendMutation
import com.datastax.driver.core.Host; //导入依赖的package包/类
public void sendMutation(Mutation mutation) {
for (PartitionUpdate partition : mutation.getPartitionUpdates()) {
Set<Host> replicas = cluster.getMetadata().getReplicas(mutation.getKeyspaceName(),
partition.partitionKey().getKey());
// in case theres multiple partitions in this mutation, with topology changes we cant assume can send
// them in batches so break them up.
Mutation toSend = new Mutation(mutation.getKeyspaceName(), partition.partitionKey());
toSend.add(partition);
for(Host h : replicas) {
InetAddress target = h.getBroadcastAddress();
StorageConnection conn = connections.get(target);
if(conn == null) {
conn = connections.computeIfAbsent(target, host -> {
StorageConnection c = new StorageConnection(host);
c.connect();
return c;
});
}
try {
conn.enqueue(toSend.createMessage(), idGen.incrementAndGet());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
示例13: StoreConnection
import com.datastax.driver.core.Host; //导入依赖的package包/类
/**
* Constructor
*
* @param nodes a list of one or more Cassandra nodes to connect to. Note
* that not all Cassandra nodes in the cluster need be
* supplied; one will suffice however if that node is
* unavailable the connection attempt will fail, even if the
* others are available.
*/
public StoreConnection(List<String> nodes) {
Cluster.Builder builder = Cluster.builder();
for (String node : nodes) {
builder.addContactPoint(node);
}
cluster = builder.build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s%n",
metadata.getClusterName());
for (Host host : metadata.getAllHosts()) {
System.out.printf("Datacenter: %s; Host: %s; Rack: %s%n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
示例14: newQueryPlan
import com.datastax.driver.core.Host; //导入依赖的package包/类
@Override
public Iterator<Host> newQueryPlan(String keyspace, Statement statement)
{
List<Host> local = new ArrayList<>(1);
List<Host> remote = new ArrayList<>(liveReplicaHosts.size());
for (Host liveReplicaHost : liveReplicaHosts)
{
if (isLocalHost(liveReplicaHost))
{
local.add(liveReplicaHost);
}
else
{
remote.add(liveReplicaHost);
}
}
Collections.shuffle(remote);
logger.debug("Using the following hosts order for the new query plan: {} | {}", local, remote);
return Iterators.concat(local.iterator(), remote.iterator());
}
示例15: resolveAllPermitted
import com.datastax.driver.core.Host; //导入依赖的package包/类
public Set<String> resolveAllPermitted(StressSettings settings)
{
Set<String> r = new HashSet<>();
switch (settings.mode.api)
{
case THRIFT_SMART:
case JAVA_DRIVER_NATIVE:
if (!isWhiteList)
{
for (Host host : settings.getJavaDriverClient().getCluster().getMetadata().getAllHosts())
r.add(host.getAddress().getHostName());
break;
}
case THRIFT:
case SIMPLE_NATIVE:
for (InetAddress address : resolveAllSpecified())
r.add(address.getHostName());
}
return r;
}